Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Efficiency and length of variable names

by moggs (Sexton)
on Mar 12, 2005 at 12:29 UTC ( [id://438901]=perlquestion: print w/replies, xml ) Need Help??

moggs has asked for the wisdom of the Perl Monks concerning the following question:

Hi Gurus, etc

In my ever ongoing quest to ensure the code I write is efficient, I was wondering about let length of variable names, subroutines, filehandles, etc.

Apart from the fact that using $count_of_database_records (slightly exaggerated) takes longer to type than $dbcnt, do the internal PERL mechanisms need to work much harder to handle them. In other words, should they be kept short?

Many thanks
Moggs
  • Comment on Efficiency and length of variable names

Replies are listed 'Best First'.
Re: Efficiency and length of variable names
by jdalbec (Deacon) on Mar 12, 2005 at 13:15 UTC
    I think this is a case of premature optimization. The length of symbols used in a program should only be an issue during compilation. In general, once the program has started, it shouldn't matter.

    In Perl I know of only two ways in which symbol lengths can affect execution. One is evaling a string (don't do that), which invokes the compiler again, and the other is using symbolic references (don't do that either), which use strict forbids.

    If you have a CGI program that's being compiled every time it executes you might want to look at converting it to mod_perl.
Re: Efficiency and length of variable names
by rozallin (Curate) on Mar 12, 2005 at 13:47 UTC

    In answer to your question, larger variable and subroutine names will certainly take up more memory than shorter ones. In most programs and scripts the difference in performance however is so minor (i.e. almost negligible) as to not be a real concern.

    If speed is of the utmost priority there are a number of programming style points which have a more significant impact on performance than the length of names; using hashes instead of linear searches, using next if to reject common cases early before you split or chop, avoiding regular expressions with many quantifiers and using s/// when concatenating strings to name a couple of examples. You might find it worth your while to read the Efficiency section (Chapter 8 Section 8.3) of Programming Perl.

    Remember, however, that there are different types of efficiency (such as space and readability/maintainability) and some of them aren't worth sacrificing for speed. Writing code with one-letter global variable names might give you a slight speed advantage now, but if you won't recognise what they symbolise when you come to re-visit the code in six months you're going to waste valuable programming time.

    -- rozallin
    rozallin@livejournal.com
Re: Efficiency and length of variable names
by gaal (Parson) on Mar 12, 2005 at 13:30 UTC
Re: Efficiency and length of variable names
by punkish (Priest) on Mar 12, 2005 at 16:03 UTC
    remember the adage -- you can perhaps write faster programs in C, but you can most always program faster in Perl.

    Perl is easier than C. Make it even easier for yourself. Unless you have a million line Amazon.com kinda code that is being hit by 28,000 users every minute, you shouldn't give a rat's batuti that you are able to shave off another 15 milliseconds from your code by converting $do_this_thing to $d.

    You might, however, want to spend an extra 35000 ms thinking about the names... for example, I try to make the variable names about the same length (without getting into unreasonable contortions) just so they line up nice and neat. Neatness in code always improves readability which directly contributes to the adage mentioned above.

    --

    when small people start casting long shadows, it is time to go to bed
      How about -- you may be able to write faster programs in C, but you will write programs faster in Perl.

      A picture is worth a thousand words, but takes 200K.
Re: Efficiency and length of variable names
by demerphq (Chancellor) on Mar 12, 2005 at 22:07 UTC

    Lexical variables are resolved at compile time so their length is irrelevent beyond the time it takes to read and lex the file. Package variables are a different story. They are essentially hash lookups on the name, and in the case of Fully::Qualified::Package::Names possibily multiple hash lookups. In this case I suspect if you really tortured things you could identify a difference. For most situations you are more likely to be using primarily lexicals and the lengths of your package variabels will have a negligable effect.

    The short of it: stick with lexicals and it isn't an issue at all.

    ---
    demerphq

Re: Efficiency and length of variable names
by jhourcle (Prior) on Mar 12, 2005 at 23:05 UTC

    I find that any time savings that may occur from writing shorter variables names almost always results in time lost should I ever need to come back and review /change / whatever the program later. So, to compensate for the shorter variable names, I need to add extra comments about what the section of code is doing. Therefore, I've lost any particular advantage, from a purely human standpoint.

    My rule of thumb is to shorten anything extraneous, but not to make it difficult to understand. I avoid abbreviations, except for specific ones that I've just gotten used to over the years (like 'db'). In your particular case, I probably would have gone with the variable name $record_count. If I had more than one type of record that the program was dealing with, then I might use something longer, like $db_record_count

    If the problem is the time that it takes to type, you're probably be better off taking a typing class. (I wish I had taken one in high school ... I could get a whole lot more done in a day if I typed faster, or with better accuracy ... the only reason I have any real speed now is from mudding during my undergrad years)

    If you're really worried about execution time, memory usage, etc, I'd recommend taking a class in assembly -- even if you're not coding directly in assembly, you can learn more about how things are handled in the processor, so you can make other optimizations. For instance, compare the following two loops:

    for ( my $i = $number_of_iterations; $i-- ; ) { }

    vs.

    for ( my $i = 0; $i++ < $number_of_iterations; ) { }

    The first one's going to be faster, because the second one is doing an inequality, and it's not against 0 (which requires an extra subtraction). Unless the program is going to run on thousands or millions of machines, it's typically cheaper to upgrade the machines, than to spend the time squeezing cycles out of the code. (of course, if it's not to be run on machines you own, the company might not decide ieven then it's worth optimizing)

Re: Efficiency and length of variable names
by TedPride (Priest) on Mar 12, 2005 at 23:32 UTC
    In terms of memory and processing time, there's so little difference between $a and $a_variable_with_long_name that the time you've spent on this discussion is already greater than all the time you'll ever save if all your programs instantly rewrote to one-character variable names. However, there's a much bigger impact on typing time and code readability, and it's only this you should worry about. Variables should follow a standard naming system (regular variables all lowercase, constant variables all uppercase, underscores to separate words inside variable names - or whatever), there should be proper use of scope, and the actual length of each variable name depends entirely on your preferences. I prefer short variable names myself, whenever possible without compromising readability, and I rarely use a variable name longer than ten characters. I just dislike having my lines of code twice as long as they need to be.
Re: Efficiency and length of variable names
by dws (Chancellor) on Mar 13, 2005 at 05:19 UTC

    in my ever ongoing quest to ensure the code I write is efficient, ...

    If you're down to worrying about the length of variable names, might it be possible that you're not using your time efficiently?

Re: Efficiency and length of variable names
by kings (Sexton) on Mar 13, 2005 at 21:21 UTC
    Regarding typing lengthy identifiers... You can increase your typing efficiency by making full use of your editor's capabilities. For example, the GNU Emacs editor allows you to expand the prefix of a string (say, a variable name) by typing the first few letters of the string, holding down the Alt key, and typing /. Emacs expands the prefix by looking for matches within the current editing buffer, then other buffers. This may sound awkward at first glance, but it quickly becomes second nature. It has saved me much typing effort through the years, and as a result I don't skimp on variable name length (within reason). See emacs "dynamic abbrevs" for more info.
Re: Efficiency and length of variable names
by moggs (Sexton) on Mar 20, 2005 at 23:57 UTC
    Many thanks for all these comments, I have much to think about.

    Cheers

    Moggs!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://438901]
Approved by rozallin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-24 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found