Performance: Partial Classes vs Inheritence?
-
Hi! I have a project where we will be generating base classes (basically just data carriers) and then implementing logic in concrete classes. Is there a runtime performance gain to using partial classes över using inheritance (or the other way around)? Alt1:
public class concreteClass : baseClass { //...logic } public class baseClass { //...properties }
Alt2:public partial class myClass // filename myClass.cs { //...logic } public partial class myClass // filename myClass.base.cs { //...properties }
-
Hi! I have a project where we will be generating base classes (basically just data carriers) and then implementing logic in concrete classes. Is there a runtime performance gain to using partial classes över using inheritance (or the other way around)? Alt1:
public class concreteClass : baseClass { //...logic } public class baseClass { //...properties }
Alt2:public partial class myClass // filename myClass.cs { //...logic } public partial class myClass // filename myClass.base.cs { //...properties }
matsnas wrote:
Is there a runtime performance gain to using partial classes över using inheritance (or the other way around)?
They are designed for different purposes. If you use partial classes rather than inherit you will create code bloat. This will increase the size of the asseblies and the memory footprint of the application. This increased memory usage could actually degrade performance.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
matsnas wrote:
Is there a runtime performance gain to using partial classes över using inheritance (or the other way around)?
They are designed for different purposes. If you use partial classes rather than inherit you will create code bloat. This will increase the size of the asseblies and the memory footprint of the application. This increased memory usage could actually degrade performance.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
Thanks for the quick reply, but could you please explain how this becomes code bloat? I'm not talking about replacing inheritance generally by the use of partial classes (below called PC) only for the data carrier objects that our code generator creates. The idea of PC is to enable different user to work on the same object in different files, comp ASP.NET and code behind, and basically that's what we'll be doing. The code generator will generate the boring propertie classes and we'll write the class logic. As I see it I'll be writing the same amount of code (properties and logic) and files(PC or base&subclass) either way?
-
Thanks for the quick reply, but could you please explain how this becomes code bloat? I'm not talking about replacing inheritance generally by the use of partial classes (below called PC) only for the data carrier objects that our code generator creates. The idea of PC is to enable different user to work on the same object in different files, comp ASP.NET and code behind, and basically that's what we'll be doing. The code generator will generate the boring propertie classes and we'll write the class logic. As I see it I'll be writing the same amount of code (properties and logic) and files(PC or base&subclass) either way?
If you use partial classes instead of inheritance then you have to repeat the same code in many classes. Code you would have ordinarily inherited.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
If you use partial classes instead of inheritance then you have to repeat the same code in many classes. Code you would have ordinarily inherited.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
Maby I'm still unclear in what I'm trying to do or I'm totally missunderstaning you. Example: I have a product class that i subclsss in BookProdukt and FilmProduct. They both have Title and Genre as attributes but the book has ISBN number and author while the film has IMDB id and director. Both film and book should have a summary attribute that gives the Title and ISBN or IMDB number. I would make a general product class that implements the title and genre attributes and two inhereting classes that implements the other respective attributes. Thus:
Product - Title - Genre - Overrideble summary BookProduct - ISBN - Author - Overrides summary (returns ISBN + " " + Title) FilmProduct - IMDB - Director - Overrides summary (returns IMDB + " " + Title)
As these objects are populated from a database I have a code generator that can create a file with the propertes for these attributes. As the generator cannot determine how the logic for the summary should be put together I need a way to seperate the logic (summary) from the data (Title, ISBN, etc) in case I need to regenerate the classes and don't want to loose the logic I've coded. This can be done in (at least) two ways: Alt1:partial class FilmProduct - IMDB - Director partial class FilmProduct - Summary
Alt:class BaseFilmProduct - IMDB - Director class FilmProduct : BaseFilmProduct - Summary
Now to the original question: Is there any performace gain in using either of the two alternatives? -
Hi! I have a project where we will be generating base classes (basically just data carriers) and then implementing logic in concrete classes. Is there a runtime performance gain to using partial classes över using inheritance (or the other way around)? Alt1:
public class concreteClass : baseClass { //...logic } public class baseClass { //...properties }
Alt2:public partial class myClass // filename myClass.cs { //...logic } public partial class myClass // filename myClass.base.cs { //...properties }
Partial classes provide no benefit at runtime, they are a compile-time only feature of the language. At compile time, the compiler takes the collection of partial classes for the type (in your example above, this would be the two partial myClass bodies from myClass.cs and myClass.base.cs) and compiles them to a single runtime type. As Colin pointed out, partial classes were designed to solve a different set of problems than inheritance and shouldn't be used in place of inheritance. Partial classes are great when you want to isoloate groups of functionality for the same class (like separating all of the event handler logic) or need to provide the ability for multiple people to work in the same type at the same time without stepping on each other's changes. The partial class can be in any file, but must be named the same as the other partial classes for that type. This prevents the same partial class from being used by other partial classes. This is what Colin meant by "code bloat". If you have multiple classes that need to share functionality, putting it in a partial class will require multiple partial classes with the same code in each, which will create a lot of problems beyond code bloat (think about fixing the same bug in multiple locations). Inheritance was designed to provide common characteristics and behavior for multiple derived (inherited) types. The benefit here is that these common traits only need to be written and compiled once.
----------------------------- In just two days, tomorrow will be yesterday.
-
Maby I'm still unclear in what I'm trying to do or I'm totally missunderstaning you. Example: I have a product class that i subclsss in BookProdukt and FilmProduct. They both have Title and Genre as attributes but the book has ISBN number and author while the film has IMDB id and director. Both film and book should have a summary attribute that gives the Title and ISBN or IMDB number. I would make a general product class that implements the title and genre attributes and two inhereting classes that implements the other respective attributes. Thus:
Product - Title - Genre - Overrideble summary BookProduct - ISBN - Author - Overrides summary (returns ISBN + " " + Title) FilmProduct - IMDB - Director - Overrides summary (returns IMDB + " " + Title)
As these objects are populated from a database I have a code generator that can create a file with the propertes for these attributes. As the generator cannot determine how the logic for the summary should be put together I need a way to seperate the logic (summary) from the data (Title, ISBN, etc) in case I need to regenerate the classes and don't want to loose the logic I've coded. This can be done in (at least) two ways: Alt1:partial class FilmProduct - IMDB - Director partial class FilmProduct - Summary
Alt:class BaseFilmProduct - IMDB - Director class FilmProduct : BaseFilmProduct - Summary
Now to the original question: Is there any performace gain in using either of the two alternatives?Hi, my first reaction would be: partial classes vs inheritance offers same performance; but try to stay away from virtual methods: using these costs more. I suggest you create a small test setup (say a short sequence of calls, running a million times in a for loop) to compare the alternatives you are considering; if a simple test does not reveal relevant difference, there probably is nothing in it. :)
Luc Pattyn [My Articles]
-
Maby I'm still unclear in what I'm trying to do or I'm totally missunderstaning you. Example: I have a product class that i subclsss in BookProdukt and FilmProduct. They both have Title and Genre as attributes but the book has ISBN number and author while the film has IMDB id and director. Both film and book should have a summary attribute that gives the Title and ISBN or IMDB number. I would make a general product class that implements the title and genre attributes and two inhereting classes that implements the other respective attributes. Thus:
Product - Title - Genre - Overrideble summary BookProduct - ISBN - Author - Overrides summary (returns ISBN + " " + Title) FilmProduct - IMDB - Director - Overrides summary (returns IMDB + " " + Title)
As these objects are populated from a database I have a code generator that can create a file with the propertes for these attributes. As the generator cannot determine how the logic for the summary should be put together I need a way to seperate the logic (summary) from the data (Title, ISBN, etc) in case I need to regenerate the classes and don't want to loose the logic I've coded. This can be done in (at least) two ways: Alt1:partial class FilmProduct - IMDB - Director partial class FilmProduct - Summary
Alt:class BaseFilmProduct - IMDB - Director class FilmProduct : BaseFilmProduct - Summary
Now to the original question: Is there any performace gain in using either of the two alternatives?Oh... I see. I understand what you are saying now. Go with the partial class idea.
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website