Static constructors cause a performance overhead?

后端 未结 4 1216
轮回少年
轮回少年 2020-12-29 07:55

Recently read in a article on dotnetpearls.com here saying that static ctors take a substantial amount of perfomance hit.

Could not fathom why?

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 08:13

    Well I've just replicated his test.

    For 1000000000 iterations with a DEBUG build I get:

    • 4s for his static class with a static constructor
    • 3.6s same class with commented out static constructor
    • 2.9s with the class non-static (and creating an instance before the iteration) with either a static constructor or not

    The same with a RELEASE build does highlight a difference:

    • Static class with static constructor: 4046.875ms
    • Static class with no static constructor: 484.375ms
    • Instance with static constructor: 484.375ms
    • Instance with no static constructor: 484.375ms

提交回复
热议问题