in reply to Does the size of a variable's name affect memory?

Hm, none of the answers so far have gone into detail pedantically enough for me tonight. Here's an gutsier explanation that is mostly true.

It depends on whether that's a global variable or a lexical variable. Perl doesn't optimize away any used variable names completely, but it does only store a single copy of lexical variable names in the appropriate lexical pad. All lexical variable accesses involve indexed lookups into the appropriate lexical value pad.

Global symbols are different, and the appropriate op in the optree keeps around the name of the symbol for a runtime lookup. That means if you refer to a global variable three times in your program, you'll have at least three more copies of its name in memory.

  • Comment on Re: Does the size of a variable's name affect memory?

Replies are listed 'Best First'.
Re^2: Does the size of a variable's name affect memory?
by Anonymous Monk on Feb 17, 2005 at 09:23 UTC
    Indeed. So, to answer the OP's question, yes, the second program will take more memory than the first one. About 25 bytes more, as your variable name contains 25 extra characters. But that's not a "considerable" amount, considering that what's on the right hand side of the assignment, the constant 1, already takes 20 bytes.
Re^2: Does the size of a variable's name affect memory?
by hv (Prior) on Feb 17, 2005 at 13:22 UTC

    Further to this, references to lexical variables are compiled to an index at compile time. References to global (package) variables involve looking up the names at runtime, and that involves hashing and string equality checks on the full name of the variable.

    While the cost of such things would not normally be significant, it is an area to consider at the point you find you need to optimise.

    Hugo