Performance of using static methods vs instantiating the class containing the methods

后端 未结 8 1696
别跟我提以往
别跟我提以往 2020-11-30 07:31

I\'m working on a project in C#. The previous programmer didn\'t know object oriented programming, so most of the code is in huge files (we\'re talking around 4-5000 lines)

8条回答
  •  庸人自扰
    2020-11-30 08:08

    From here, a static call is 4 to 5 times faster than constructing an instance every time you call an instance method. However, we're still only talking about tens of nanoseconds per call, so you're unlikely to notice any benefit unless you have really tight loops calling a method millions of times, and you could get the same benefit by constructing a single instance outside that loop and reusing it.

    Since you'd have to change every call site to use the newly static method, you're probably better spending your time on gradually refactoring.

提交回复
热议问题