What overhead is associated with the extension method at runtime? (.NETWORK)

Possible duplicate:
Extension Method Performance

In a data crunching application that involves CPU and / or memory access, is overhead one way to expand a string? Is this something more than a regular function call, or is it just an abstraction of the compiler / IDE? For example, will the following function be warned if it was called several thousand times per second:

public static void WriteElementString(this XmlTextWriter writer, string name, int data) { writer.WriteElementString(name, data.ToString()); } 
+7
extension-methods
Sep 18 '09 at 6:31
source share
2 answers

No overhead. This is just a static method with different syntax. The generated IL is a common call.

In other words, the overhead for your extension method is exactly the same for

 writer.WriteElementString(name, data); 

as if you just called

 XmlWriterExtensions.WriteElementString(writer, name, data); 

... because the generated IL will be exactly the same.

In terms of productivity, β€œover several thousand times per second” is nothing. The overhead for having an extra stack level will be completely negligible at that level ... even if the method is not included, which, in my opinion, is very likely in this case.

However, the normal rule of execution applies: all guesses until you have measured. Or at least the actual blow in this case is a hunch; "extension methods are just ordinary methods with syntactic sugar in the compiler" - this is not speculation.

+18
Sep 18 '09 at 6:33
source share

No overhead at all, its just syntactic sugar, its simple compiler abstraction.

+3
Sep 18 '09 at 6:34
source share



All Articles