"perl's way" is better(?),faster, cheaper, shorter(just), and preferable in every way(opinion?).
c:\test>perl -MBenchmark=cmpthese my ($a,$b)=('a'x1000,'b'x1000); cmpthese( -10, { PERL=> sub{ ($a, $b) = ($b, $a); }, C => sub{ my $t=$a, $a=$b, $b=$t; }, XOR => sub{ $a^=$b^=$a^=$b; }, } ); ^Z Benchmark: running C, PERL, XOR, each for at least 10 CPU seconds... C: 10 wallclock secs (10.01 usr + 0.00 sys = 10.01 CPU) @ 19 +7752.07/s (n=1980487) PERL: 10 wallclock secs (10.30 usr + 0.00 sys = 10.30 CPU) @ 12 +1439.73/s (n=1250222) XOR: 10 wallclock secs (10.03 usr + -0.01 sys = 10.02 CPU) @ 33 +5113.02/s (n=3356492) Rate PERL C XOR PERL 121440/s -- -39% -64% C 197752/s 63% -- -41% XOR 335113/s 176% 69% -- c:\test>
If performance isn't a concern I use list assignment, if it is, I'd use the XOR method.
Examine what is said, not who speaks.
In reply to Re: Re: swapping vars, two ways, 2 results...
by BrowserUk
in thread swapping vars, two ways, 2 results...
by Sihal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |