Does how you name a variable have any impact on the memory usage of an application?

前端 未结 7 1356
灰色年华
灰色年华 2021-02-20 09:05

Declaring a variable name, how much (if at all) impact does the length of its name have to the total memory of the application? Is there a maximum length anyway? Or are we free

相关标签:
7条回答
  • 2021-02-20 09:13

    It has no impact in a compiled language.

    0 讨论(0)
  • 2021-02-20 09:20

    In Python, the names appear to be collected into a number of simple tables; each name appears exactly once in each code object.The names have no impact on performance.

    For statistical purposes, I looked at a 20 line function that was a solution to Project Euler problem 15. This function created a 292-byte code object. It used 7 distinct names in the name table. You'd have to use 41-character variable names to double the size of the byte-code file.

    That would be the only impact -- insanely large names might slow down load time for your module.

    0 讨论(0)
  • 2021-02-20 09:22

    It depends on the language, actually.

    If you're using C++ or C, it has no impact.

    If you're using an interpreted language, you're passing the source code around, so it can have a dramatic impact.

    If you're using a compiled language that compiles to an intermediate language, such as Java or any of the .NET languages, then typically the variable names, class names, method names, etc are all part of the IL. Having longer method names will have an impact. However, if you later run through an obfuscator, this goes away, since the obfuscator will rename everything to (typically) very short names. This is why obfuscation often gives performance impacts.

    However, I will strongly suggest using long, descriptive variable/method/class names. This makes your code understandable, maintainable, and readable - in the long run, that far outweighs any slight perf. benefit.

    0 讨论(0)
  • 2021-02-20 09:22

    Actually in ASP.NET long variable names for controls and master pages do add to the size of the generated HTML. This will add some insignificant extra memory to buffer the output stream, but the effect will be most noticed in the extra few hundred bytes your sending over the network.

    0 讨论(0)
  • 2021-02-20 09:26

    As stated by others, variable names disappear in compiled languages. I believe that local variable names in .Net may be discarded. But generally speaking, even in an interpreted language, the memory consumption of variable names is negligible, especially in light of the advantages of good variable names.

    0 讨论(0)
  • 2021-02-20 09:27

    MSVC++ truncates variable names to 255 characters. Variable name length has no impact on compiled code size.

    0 讨论(0)
提交回复
热议问题