Incidentally, the original obfuscated code is pretty damn fast
if you expect to return False most of the time.

                       Rate if_else - (F) space_ship - (F) space_ship (T)
ternary - (F) if_else - (T) ret_1_or_ret_0 (T) ternary - (T) ret_1_or_ret_0
(F) (control) (F) (control) (T)
if_else - (F)       94340/s            --              -3%            -4%
-5%           -5%                -5%           -6%                -8%
-25%          -27%
space_ship - (F)    97087/s            3%               --            -1%
-3%           -3%                -3%           -3%                -5%
-22%          -25%
space_ship (T)      97847/s            4%               1%             --
-2%           -2%                -2%           -3%                -4%
-22%          -24%
ternary - (F)       99602/s            6%               3%             2%
--            0%                -0%           -1%                -3%
-20%          -23%
if_else - (T)       99602/s            6%               3%             2%
0%            --                -0%           -1%                -3%
-20%          -23%
ret_1_or_ret_0 (T)  99602/s            6%               3%             2%
0%            0%                 --           -1%                -3%
-20%          -23%
ternary - (T)      100402/s            6%               3%             3%
1%            1%                 1%            --                -2%
-20%          -22%
ret_1_or_ret_0 (F) 102249/s            8%               5%             4%
3%            3%                 3%            2%                 --
-18%          -21%
(control) (F)      125000/s           32%              29%            28%
25%           25%                25%           25%                22%
--           -3%
(control) (T)      128866/s           37%              33%            32%
29%           29%                29%           28%                26%
3%            --

#!/usr/bin/perl

use Benchmark qw(cmpthese) ;

print 'if_else - true: ' . true(\&if_else) . "\n";
print 'if_else - false: ' . false(\&if_else) . "\n";
print 'return_1_or_return_0 - true: ' . true(\&return_1_or_return_0) .
"\n";
print 'return_1_or_return_0 - false: ' . false(\&return_1_or_return_0) .
"\n";
print 'space_ship - true: ' . true(\&space_ship) . "\n";
print 'space_ship - false: ' . false(\&space_ship) . "\n";
print 'ternary - true: ' . true(\&ternary) . "\n";
print 'ternary - false: ' . false(\&ternary) . "\n";
print "\n\n";

$count = 500000;
cmpthese($count, {
    '(control) (T)' => 'true(\&nothing)',
    '(control) (F)' => 'false(\&nothing)',
    'if_else - (T)' => 'true(\&if_else)',
    'if_else - (F)' => 'false(\&if_else)',
    'ret_1_or_ret_0 (T)' => 'true(\&return_1_or_return_0)',
    'ret_1_or_ret_0 (F)' => 'false(\&return_1_or_return_0)',
    'space_ship (T)' => 'true(\&space_ship)',
    'space_ship - (F)' => 'false(\&space_ship)',
    'ternary - (T)' => 'true(\&ternary)',
    'ternary - (F)' => 'false(\&ternary)',
});

sub true {
   my ($rSub) = @_;
   $rSub->(1, 1);
}

sub false {
   my ($rSub) = @_;
   $rSub->(0, 1);
}

sub nothing {
my ($one, $two) = @_;
}

sub if_else {
my ($one, $two) = @_;
if ($one == $two) { return 1 } else { return 0 }
}

sub return_1_or_return_0 {
my ($one, $two) = @_;
return 1 if ($one == $two) or return 0;
}

sub space_ship {
my ($one, $two) = @_;
return ($one == $two) <=> 0;
}

sub ternary {
my ($one, $two) = @_;
return ($one == $two) ? 1 : 0;
}

-------------------------------------------------------------------------------------

The entrepreneur is essentially a visualizer and an
actualizer... He can visualize something and when he
visualizes it he sees exactly how to make it happen.
-- Robert L. Schwartz




**********************************************************************************
This email may contain confidential material. If you were not an
intended recipient, please notify the sender and delete all copies.
We may monitor email to and from our network.

Reply via email to