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

Faster with my keyword or no ?

by abdan (Novice)
on Nov 24, 2021 at 00:39 UTC ( #11139066=perlquestion: print w/replies, xml ) Need Help??

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

Is having my keyword precedes identifier var. speed up the Perl performance compare to otherwise ?

Replies are listed 'Best First'.
Re: Faster with my keyword or no ?
by BillKSmith (Monsignor) on Nov 24, 2021 at 04:18 UTC
    We strongly recommend that you always use strict in every script. Under strict, you must declare all your variables (my for lexical, our for global). Without strict, all variables default to 'package'. Your question becomes "Which type of variable is faster?" I doubt that it makes much difference.

    In general, It is better to have a program that is easy to maintain than it is to have one that is marginally faster. ('strict' and 'my' offer several advantages to the maintainer.) The usual recommendation is to not even consider execution speed until you have a complete program that works correctly, but is unacceptably slow. At that point, profile it to identify the problem area. Write several versions of this code. Benchmark each with realistic data. (You will almost always be surprised by the result). Resist the temptation to make "easy changes" to speed-up other sections. Even a big improvement will make little difference in your complete program.

    Bill
Re: Faster with my keyword or no ?
by eyepopslikeamosquito (Bishop) on Nov 24, 2021 at 08:39 UTC

    Perl's my lexical variables are not slow. They've been very heavily used in millions of lines of Perl code for many years now.

    Instead of fretting about their performance, I suggest you spend your time understanding why they should be used in just about every Perl script you write. To get you started down that path, I've provided a few references below. After understanding the why, please feel free to post your code (using strict and my) for review.

    If you have any performance concerns in any of your code, please present specific benchmark results first.

    Why Using Strict and My Lexical Variables is Recommended

    Stick to using only lexical variables (my) unless you genuinely need the functionality that only a package or punctuation variable can provide.

    Using non-lexical variables increases the "coupling" of your code. If two otherwise unrelated sections of code both use a package variable, those two pieces of code can interact with each other in very subtle ways, just by the way they each interact with that shared variable. In other words, without full knowledge of every other piece of code that is called from a particular statement, it is impossible to know whether the value of a given non-lexical variable will somehow be changed by executing that statement.

    -- Damian Conway in Perl Best Practices (Chapter 5: Variables)

Re: Faster with my keyword or no ?
by hippo (Bishop) on Nov 24, 2021 at 09:35 UTC

    How can you tell if option A is faster than option B? Benchmark!


    🦛

Re: Faster with my keyword or no ?
by LanX (Sage) on Nov 24, 2021 at 09:26 UTC
Re: Faster with my keyword or no ?
by cavac (Curate) on Nov 30, 2021 at 15:29 UTC

    After more than a decade of optimizing the perl interpreter for modern usage (e.g. use strict;), it seems Perl is significantly faster when using "my". Here are my test results:

    cavac@earthrise:~/src/temp$ ./speedtest.sh Speedtest bash script: #!/bin/bash echo "Speedtest bash script:" cat speedtest.sh echo echo Perl version: perl -v | grep version echo echo "Warming up CPU to operating temp" cat speedtest_warmup.pl time perl speedtest_warmup.pl echo time perl speedtest_warmup.pl echo "########### Modern perl: strict and warnings enabled ########### +#" cat speedtest1.pl echo time perl speedtest1.pl echo "########### Outdated: my variables, but no strict and warnings # +#######" cat speedtest2.pl echo time perl speedtest2.pl echo "########### Noob perl: no strict, warnings or my variables ##### +##" cat speedtest3.pl echo time perl speedtest3.pl Perl version: This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-li +nux Warming up CPU to operating temp #!/usr/bin/env perl # Just to get system load and CPU temp up to test conditions use strict; use warnings; my $total = 0; for(my $i = 0; $i < 200_000_000; $i++) { $total++; } real 0m14,558s user 0m14,543s sys 0m0,004s real 0m14,845s user 0m14,833s sys 0m0,008s ########### Modern perl: strict and warnings enabled ############ #!/usr/bin/env perl # STRICT & WARNINGS + MY # use strict; use warnings; my $total = 0; for(my $i = 0; $i < 300_000_000; $i++) { $total++; } real 0m22,095s user 0m22,086s sys 0m0,004s ########### Outdated: my variables, but no strict and warnings ####### +# #!/usr/bin/env perl # MY WITHOUT STRICT AND WARNINGS # my $total = 0; for(my $i = 0; $i < 300_000_000; $i++) { $total++; } real 0m21,943s user 0m21,938s sys 0m0,000s ########### Noob perl: no strict, warnings or my variables ####### #!/usr/bin/env perl # NOOBIE VERSION WITHOUT STRICT; WARNINGS AND MY # $total = 0; for($i = 0; $i < 300_000_000; $i++) { $total++; } real 0m41,117s user 0m41,100s sys 0m0,008s

    perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'
Re: Faster with my keyword or no ?
by ikegami (Pope) on Nov 30, 2021 at 15:39 UTC

    Does it matter how fast it is if the program doesn't run without it? Always use use strict; use warnings;.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2021-12-04 18:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?