<snip>
> Drieux might provide us with some benchmarks about which one's faster
> these days... ;-)
</snip>

I beat him to it.  Interestingly enough, I either have a bug in my code
(and I don't think I do) or xor is dangerous in relation to negative
numbers (without the integer pragma).  The results seem to favor xor
except for long strings, but since xor is dangerous for negative doubles
and temp came in a close second or first every time I would suggest the
use of a temporary variable if you are concerned with speed.

<output>
Testing string type swaps
        list
                a = [this is one] :: b = [this is two]
                a = [this is two] :: b = [this is one]
        xor
                a = [this is one] :: b = [this is two]
                a = [this is two] :: b = [this is one]
        c_inout
                a = [this is one] :: b = [this is two]
                a = [this is two] :: b = [this is one]
        temp
                a = [this is one] :: b = [this is two]
                a = [this is two] :: b = [this is one]
Testing integer type swaps
        list
                a = [123] :: b = [-345]
                a = [-345] :: b = [123]
        xor
                a = [123] :: b = [-345]
                a = [4294966951] :: b = [123]
        c_inout
                a = [123] :: b = [-345]
                a = [-345] :: b = [123]
        temp
                a = [123] :: b = [-345]
                a = [-345] :: b = [123]
Testing double type swaps
        list
                a = [123.345] :: b = [-345.457]
                a = [-345.457] :: b = [123.345]
        xor
                a = [123.345] :: b = [-345.457]
                a = [4294966951] :: b = [123]
        c_inout
                a = [123.345] :: b = [-345.457]
                a = [-345.457] :: b = [123.345]
        temp
                a = [123.345] :: b = [-345.457]
                a = [-345.457] :: b = [123.345]
Benchmarking string swaps
            Rate    list c_inout    temp     xor
list    241920/s      --    -16%    -54%    -55%
c_inout 287630/s     19%      --    -45%    -47%
temp    527651/s    118%     83%      --     -2%
xor     539312/s    123%     88%      2%      --
Benchmarking long_string swaps
            Rate    list c_inout     xor    temp
list    177932/s      --    -15%    -40%    -44%
c_inout 208521/s     17%      --    -29%    -35%
xor     294637/s     66%     41%      --     -8%
temp    319670/s     80%     53%      8%      --
Benchmarking integer swaps
            Rate    list c_inout    temp     xor
list    514623/s      --    -13%    -40%    -41%
c_inout 594074/s     15%      --    -31%    -32%
temp    863985/s     68%     45%      --     -1%
xor     872933/s     70%     47%      1%      --
Benchmarking double swaps
            Rate    list c_inout    temp     xor
list    508858/s      --     -7%    -40%    -42%
c_inout 549703/s      8%      --    -35%    -38%
temp    843806/s     66%     54%      --     -5%
xor     883833/s     74%     61%      5%      --
<output>

<code>
#!/usr/bin/perl

use strict;
use warnings;
use Benchmark;
use Inline 'C';

my $a;
my $b;

my %sub = (
        xor => sub {
                $a ^= $b;
                $b ^= $a;
                $a ^= $b;
        },
        list => sub {
                ($a, $b) = ($b, $a);
        },
        temp => sub {
                my $temp = $a;
                $a       = $b;
                $b       = $temp;
        },
        c_inout => sub {
                c_inout($a, $b);
        }
);

my %run = (
        integer      => [123,     -345],
        double       => [123.345, -345.457],
        string       => ["this is one", "this is two"],
        long_string  => [
                "this is the first very long string, and a very long string it is
indead for I remember few strings longer or as long than this one, maybe
way back in aught one the was a string close to this size, but I think
that was just an optical illusion because this string is just way too
freaking huge to be compared with paltry little strings like 'this is
one' or 'this is two' and the like.",
                "this is the second very long string, and a very long string it is
indead for I remember few strings longer or as long than this one, maybe
way back in aught one the was a string close to this size, but I think
that was just an optical illusion because this string is just way too
freaking huge to be compared with paltry little strings like 'this is
one' or 'this is two' and the like.",
        ],
);

foreach my $run (keys %run) {
        next if $run eq 'long_string';
        print "Testing $run type swaps\n";
        foreach my $sub (keys %sub) {
                print "\t$sub\n";
                ($a, $b) = ($run{$run}[0], $run{$run}[1]);
                print "\t\ta = [$a] :: b = [$b]\n";
                $sub{$sub}->();
                print "\t\ta = [$a] :: b = [$b]\n";
        }
}

foreach my $run (keys %run) {
        print "Benchmarking $run swaps\n";
        ($a, $b) = ($run{$run}[0], $run{$run}[1]);
        my $result = Benchmark::timethese(0, \%sub, 'none');
        Benchmark::cmpthese($result);
}

__DATA__

__C__

void c_inout(SV* a, SV* b) {
        SV* temp;

        temp = sv_mortalcopy(a);
        sv_setsv(a, b);
        sv_setsv(b, temp);
}
</code>

 
-- 
Today is Setting Orange the 59th day of Confusion in the YOLD 3168
Hail Eris!

Missile Address: 33:48:3.521N  84:23:34.786W


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to