"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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.