For example, check out these two ways to swap two variables:
$x ^= $y ^= $x ^= $y;
($x, $y) = ($y, $x);
It might surprise you to learn that these aren't even equivalent. The XORs won't work with references.
-sauoq
"My two cents aren't worth a dime.";
| [reply] [Watch: Dir/Any] [d/l] |
Er by far faster and easier to read to someone inexperienced in perl is
my $tmp=$x;
$x=$y;
$y=$tmp;
Premature optimization is evil, but then again so is false lazyness
Yves / DeMerphq
---
Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)
| [reply] [Watch: Dir/Any] [d/l] |
| [reply] [Watch: Dir/Any] |
Im curious what you call signifigant, and also how you benchmarked them. Please post your benchmark. In my tests, with a predeclared swap variable, I see a %50 difference when using dynamic variables, and a %25 difference when using lexical variables. Personally I call that signifigant, especially if its inside a loop. Oh the difference would probably be even more drammatic for lexicals but for the fact of having to repeatedly declare them, which wouldnt be necessary in a more realisitic situation.
use Benchmark 'cmpthese';
our $a=1;
our $b=2;
our $tmp;
print "Dynamic variables, swap benchmark.\n";
cmpthese -10, {standard => '$::tmp=$::a; $::a=$::b; $::b=$tmp;',
list => '($::a,$::b)=($::b,$::a)'
};
print "Lexical variables, swap benchmark.\n";
cmpthese -10, {
standard_my => sub{my $x=1;my $y=2; my $z =$x; $x=$y
+; $y=$z},
standard_local => sub{my $x=1;my $y=2; local $_=$x; $x=
+$y; $y=$_},
standard_global => sub{my $x=1;my $y=2; $_=$x; $x=$y; $y
+=$_},
list => sub{my $x=1;my $y=2; ($x,$y)=($y,$x)}
+,
};
__END__
Dynamic variables, swap benchmark.
Benchmark: running list, standard, each for at least 10 CPU seconds...
list: 11 wallclock secs (10.39 usr + 0.00 sys = 10.39 CPU) @ 66
+4534.94/s (n=6904518)
standard: 11 wallclock secs (10.84 usr + 0.00 sys = 10.84 CPU) @ 98
+3324.54/s (n=10662188)
Rate list standard
list 664535/s -- -32%
standard 983325/s 48% --
Lexical variables, swap benchmark.
Benchmark: running list, standard_global, standard_local, standard_my,
+
each for at least 10 CPU seconds...
list: 10 wallclock secs (10.13 usr + 0.00 sys = 10.13 CPU) @ 40
+4027.75/s (n=4090781)
standard_global:
9 wallclock secs (10.05 usr + 0.00 sys = 10.05 CPU) @ 484686.67/s (
+n=4869647)
standard_local:
10 wallclock secs (10.03 usr + 0.00 sys = 10.03 CPU) @ 402621.17/s (
+n=4038693)
standard_my:
10 wallclock secs (10.80 usr + 0.00 sys = 10.80 CPU) @ 503630.27/s (
+n=5437696)
Rate standard_local list standard_global s
+tandard_my
standard_local 402621/s -- -0% -17%
+ -20%
list 404028/s 0% -- -17%
+ -20%
standard_global 484687/s 20% 20% --
+ -4%
standard_my 503630/s 25% 25% 4%
+ --
Yves / DeMerphq
---
Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)
| [reply] [Watch: Dir/Any] [d/l] |