in reply to declaring lexical variables in shortest scope: performance?
At least on my 5.28, the difference is negligible:
use warnings; use strict; use Benchmark 'cmpthese'; cmpthese(-2, { predecl => sub { my $y; my $x; for $x (1,2,3) { $y+=$x } }, lexical => sub { my $y; for my $x (1,2,3) { $y+=$x } }, }); __END__ Rate predecl lexical predecl 9175035/s -- -1% lexical 9275893/s 1% --
But wakes in me primordial fears
Yes, I know the feeling well. But my philosophy has become: first, code so that it works, avoiding only the really obvious performance mistakes (like scanning an array instead of using a hash and the like). Then, if it's fast enough for your puproses, you're done. But if you want to optimize, remember that optimization is a science: measure the performance, identify the hotspots, benchmark the alternatives, modify the code accordingly, measure the difference in performance, and repeat until the performance becomes good enough for your purposes.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: declaring lexical variables in shortest scope: performance?
by bliako (Abbot) on Mar 31, 2020 at 11:05 UTC | |
by choroba (Cardinal) on Mar 31, 2020 at 11:27 UTC | |
by bliako (Abbot) on Mar 31, 2020 at 11:58 UTC | |
by LanX (Saint) on Mar 31, 2020 at 12:00 UTC | |
by bliako (Abbot) on Mar 31, 2020 at 12:14 UTC | |
| |
|
Re^2: declaring lexical variables in shortest scope: performance?
by Anonymous Monk on Mar 31, 2020 at 11:52 UTC | |
by haukex (Archbishop) on Mar 31, 2020 at 12:08 UTC |