After compensating for everything I could think of, XOR still won. Surprisingly, it won against the standard temp-variable swap. Incredibly, it won against a sub that simply returned the numbers in reverse order (in fact, temp swapping generally beat that, too!). I could only think that it wins because the integer math makes things simpler internally.

I tried different types of arguments, and preprocessing the argument list to force it to integer. Speedup was slight, if there was any at all, for the integer-forced versions. Perl swapping lost pretty consistently in all cases. Temp swapping and return-value swapping beat XOR for swapping strings, sometimes. I got inconsistent results.

Benchmark code follows...

use Benchmark 'cmpthese'; my @swapthese; sub xor_swap { my ($x,$y)=@swapthese; $x ^= $y; $y ^= $x; $x ^= $y; ($x,$y); } sub perl_swap { my ($x,$y)=@swapthese; ($x,$y) = ($y,$x); ($x,$y); } sub perl_i_swap { my ($x,$y)=@swapthese; ($x,$y) = ($y^0,$x^0); ($x,$y); } { my $tmp; sub tmp_swap { my ($x,$y)=@swapthese; $tmp=$x; $x=$y; $y=$tmp; ($x,$y); } sub tmp_i_swap { my ($x,$y)=@swapthese; $tmp=$x^0; $x=$y^0; $y=$tmp; ($x,$y); } } sub ret_swap { my ($x,$y)=@swapthese; ($y,$x); } sub ret_i_swap { my ($x,$y)=@swapthese; $x ^= 0; $y ^= 0; ($y,$x); } for my $S ([38,970], [38.1,970], ['foo',38], ['foo','barbell']) { @swapthese = @$S; print "Swapping @swapthese\n"; print "XOR: @{[xor_swap()]}\n"; print "Perl: @{[perl_swap()]}\n"; print "Perl I: @{[perl_i_swap()]}\n"; print "TMP: @{[tmp_swap()]}\n"; print "TMP I: @{[tmp_i_swap()]}\n"; print "RET: @{[ret_swap()]}\n"; print "RET I: @{[ret_i_swap()]}\n"; cmpthese (-2, { xor_swap => \&xor_swap, perl_swap => \&perl_swap, tmp_swap => \&tmp_swap, ret_swap => \&tmp_swap, perl_i_swap => \&perl_swap, tmp_i_swap => \&tmp_swap, ret_i_swap => \&tmp_swap, }); }

Caution: Contents may have been coded under pressure.

In reply to Re^4: Extraction of List of Coordinates by Roy Johnson
in thread Extraction of List of Coordinates by EchoAngel

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.