What's the performance difference between Partial Method and overriding a virtual method in the base class?

I am updating my code generator and have a choice between implementing the stub method as a virtual method in the base class, or a partial method in the generated code. Is there a performance difference between the two?

+1


source to share


2 answers


If you are implementing a partial method, I would expect there will be no discernible difference. C # always uses callvirt

instance methods to call, even if they are not virtual, so there will be no change.

If you don't implement the partial method, then the call itself is removed - so there never will be a ready stack, etc. This will be faster, which means it is entirely possible for the generated code to include a ridiculous number of partial method fragments: if you don't use them, they don't exist.



The big reason for using partial methods is simply that you don't need to subclass the object. You cannot declare "abstract" / "virtual" and "override" parts of a virtual method in the same class (even if they are partial). Partial methods solve this problem and the problem of ad extensibility points (without using reflection).

Very good for code generation tools; -p

+3


source


There may be a minor difference, but it should be minor unless the method is called repeatedly in a loop.

Partial methods are just syntactic sugar that allows you to split a class into multiple files.



When compiled, they are exactly the same as if all methods were in one file. In other words, the only "slowdown" you'll probably see with a partial method is a slightly longer compile time :)

EDIT: And as it says below, they are not even there if they cannot be found at compile time.

+1


source







All Articles