Does variable name length matter for performance C#?

前端 未结 6 2231
别那么骄傲
别那么骄傲 2021-02-18 22:28

I\'ve been wondering if using long descriptive variable names in WinForms C# matters for performance? I\'m asking this question since in AutoIt v3 (interpreted language) it was

6条回答
  •  忘了有多久
    2021-02-18 22:59

    It is a key difference between a compiler and an interpreter. An interpreter does an identifier name lookup while it is interpreting code. If that name lookup happens inside a loop that executes many times, the time needed for that lookup can matter. Longer names require more time.

    The C# compiler eliminates identifier names in two distinct stages. The names of local variables are erased and replaced by stack frame offsets when it compiles the source code to IL. Namespace and type names are still present in the assembly. The JIT compiler erases those, replacing them with code offsets for methods and data offsets for fields. The length of the identifier names doesn't matter here, the lookup only happens once.

    Eliminating the expense of the name lookup in an interpreter isn't hard btw. A decent interpreter tokenizes the source code, essentially a pre-compile step to make the interpreter more efficient. Such an interpreter would not have a slowdown issue with long identifier names either.

提交回复
热议问题