abdan has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Faster with my keyword or no ?
by BillKSmith (Monsignor) on Nov 24, 2021 at 04:18 UTC | |
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
| [reply] [d/l] [select] |
|
Re: Faster with my keyword or no ?
by eyepopslikeamosquito (Archbishop) 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
| [reply] |
|
Re: Faster with my keyword or no ?
by hippo (Archbishop) on Nov 24, 2021 at 09:35 UTC | |
How can you tell if option A is faster than option B? Benchmark! 🦛 | [reply] |
|
Re: Faster with my keyword or no ?
by LanX (Saint) on Nov 24, 2021 at 09:26 UTC | |
I saw old articles² explaining why lexical variables are faster than package variables.
Cheers Rolf °) or at least: "it used to be so" ²) like: Lexical variables are a bit faster too since Perl doesn’t have to deal with the symbol table
| [reply] |
|
Re: Faster with my keyword or no ?
by cavac (Prior) 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:
perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'
| [reply] [d/l] [select] |
|
Re: Faster with my keyword or no ?
by ikegami (Patriarch) 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;. | [reply] [d/l] |