HighTechTalks DotNet Forums  

c# static constructor?

Dotnet Framework (Performance) microsoft.public.dotnet.framework.performance


Discuss c# static constructor? in the Dotnet Framework (Performance) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
ms news group
 
Posts: n/a

Default c# static constructor? - 05-02-2007 , 11:45 PM






I have done some research on this issue, but still quite puzzled.
I read one article that says static constructor can be a big performance
hit, it even gives some code fragment, which, accoring to that article,
could show a big difference betweeen using static constructor or not. But
when I run his code, I found no significant diifference at all.
For this reason, I even checked the assemly code of using static
constructor, and I can not find any thing that will cause performornce
degradation.
Or course, lazy initialization is good, but not all cases really care that
much about startup time, so I doubt if the performance issue of static
constructor is exagerated.

Could someone shed some light on this?





Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: c# static constructor? - 05-03-2007 , 02:55 PM






ms news group <noret (AT) noret (DOT) com> wrote:
Quote:
I have done some research on this issue, but still quite puzzled.
I read one article that says static constructor can be a big performance
hit, it even gives some code fragment, which, accoring to that article,
could show a big difference betweeen using static constructor or not. But
when I run his code, I found no significant diifference at all.
For this reason, I even checked the assemly code of using static
constructor, and I can not find any thing that will cause performornce
degradation.
Or course, lazy initialization is good, but not all cases really care that
much about startup time, so I doubt if the performance issue of static
constructor is exagerated.

Could someone shed some light on this?
For classes with static constructors, the type initializer can't be
executed until the first time a member of the class is *actually*
referenced, rather than just at (say) the start of a method where a
member of the class *might* be referenced.

I wouldn't say it's a particularly significant performance hit even in
the most extreme situations I've looked at, but it *is* noticeable if
you have (say) a very tight loop which just calls a cheap static method
on a class with a static constructor.

Of course, this may have been optimised a bit in .NET 2.0...

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.