User Tools

Site Tools


benchperl

Benchmarks

  • split
  • for, foreach, while
  • IO::File, open, ``
  • chomp
  • string contexte
Machine de test
* Intel(R) Xeon(R) CPU           E5450  @ 3.00GHz
* Linux nsymbiose 2.6.32-5-amd64 #1 SMP Tue Jun 14 09:42:28 UTC 2011 x86_64 GNU/Linux
* This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
split

contributeurs: Jerome Gouzy, Valentin Guignon

#!/usr/bin/perl

# Pistes d'optimisations: When perl is not quite fast enough http://www.flirble.org/~nick/P/Fast_Enough/

use strict;
use warnings;

use Benchmark qw(cmpthese);
my $HSP     = "ALOMY000001\t0\t30\tALOMY000001\t97\t147\tHWI-ST132_0437:2:3:8601:8436#GCGGTC\t80\t+\t-\treg=1-147 len=147";
my $BEDPE   = "A20IIA20IIA40fAAA20"; # ce format n'est sans doute pas optimal en temps et en memoire
my $HSPPACK = pack($BEDPE, split("\t", $HSP));

sub split_str
{
    my $str = $HSP;
    my (@a_)= split("\t", $str);
}

sub split_str_noalloc
{
    my $str = $HSP;
    split("\t", $str); # soit utiliser @_ ou plus propre allouer @a_ avant la boucle qui fait le split
}


sub split_char
{
    my $str = $HSP;
    my (@a_)= split('\t', $str);
}


sub split_regex
{
    my $str = $HSP;
    my (@a_)= split(/\t/, $str);
}

sub split_unpack
{
    my $strpack = $HSPPACK;
    my (@a_)= unpack($BEDPE, $strpack);
}

sub split_str_ref
{
    my $str = $HSP;
    my $r_ = [split("\t", $str)];
}

sub split_char_ref
{
    my $str = $HSP;
    my $r_ = [split('\t', $str)];
}


sub split_regex_ref
{
    my $str = $HSP;
    my $r_ = [split(/\t/, $str)];
}

sub split_unpack_ref
{
    my $strpack = $HSPPACK;
    my $r_ = [unpack($BEDPE, $strpack)];
}


for(my $i=0; $i < 5; ++$i)
{
    cmpthese(
        -1,
        {
            split_str         => \&split_str,
            split_noalloc     => \&split_str_noalloc,
            split_char        => \&split_char,
            split_regex       => \&split_regex,
            split_unpack      => \&split_unpack,
            split_str_ref     => \&split_str_ref,
            split_char_ref    => \&split_char_ref,
            split_regex_ref   => \&split_regex_ref,
            split_unpack_ref  => \&split_unpack_ref,
        },
    );
}
   
                    Rate split_regex split_str split_char split_unpack split_regex_ref split_unpack_ref split_char_ref split_str_ref split_noalloc
split_regex      312785/s          --       -3%        -4%         -14%            -23%             -23%           -23%          -24%          -51%
split_str        321254/s          3%        --        -1%         -12%            -21%             -21%           -21%          -22%          -50%
split_char       324436/s          4%        1%         --         -11%            -20%             -20%           -20%          -21%          -49%
split_unpack     364089/s         16%       13%        12%           --            -10%             -10%           -10%          -12%          -43%
split_regex_ref  405735/s         30%       26%        25%          11%              --               0%            -0%           -2%          -36%
split_unpack_ref 405735/s         30%       26%        25%          11%              0%               --            -0%           -2%          -36%
split_char_ref   405735/s         30%       26%        25%          11%              0%               0%             --           -2%          -36%
split_str_ref    412559/s         32%       28%        27%          13%              2%               2%             2%            --          -35%
split_noalloc    637155/s        104%       98%        96%          75%             57%              57%            57%           54%            --
                     Rate split_str split_char split_regex split_unpack split_regex_ref split_unpack_ref split_str_ref split_char_ref split_noalloc
split_str        315076/s        --        -2%         -3%         -10%            -18%             -20%          -21%           -22%          -51%
split_char       321255/s        2%         --         -1%          -9%            -17%             -19%          -19%           -20%          -51%
split_regex      325993/s        3%         1%          --          -7%            -15%             -17%          -18%           -19%          -50%
split_unpack     351623/s       12%         9%          8%           --             -9%             -11%          -12%           -13%          -46%
split_regex_ref  385506/s       22%        20%         18%          10%              --              -2%           -3%            -4%          -41%
split_unpack_ref 394569/s       25%        23%         21%          12%              2%               --           -1%            -2%          -39%
split_str_ref    398221/s       26%        24%         22%          13%              3%               1%            --            -1%          -39%
split_char_ref   401943/s       28%        25%         23%          14%              4%               2%            1%             --          -38%
split_noalloc    649176/s      106%       102%         99%          85%             68%              65%           63%            62%            --
                     Rate split_char split_str split_regex split_unpack split_char_ref split_regex_ref split_unpack_ref split_str_ref split_noalloc
split_char       312785/s         --       -1%         -2%         -14%           -20%            -21%             -22%          -23%          -52%
split_str        317021/s         1%        --         -1%         -13%           -19%            -20%             -21%          -22%          -51%
split_regex      319956/s         2%        1%          --         -12%           -18%            -20%             -20%          -21%          -51%
split_unpack     364089/s        16%       15%         14%           --            -7%             -9%              -9%          -10%          -44%
split_char_ref   390981/s        25%       23%         22%           7%             --             -2%              -3%           -4%          -40%
split_regex_ref  398221/s        27%       26%         24%           9%             2%              --              -1%           -2%          -39%
split_unpack_ref 401944/s        29%       27%         26%          10%             3%              1%               --           -1%          -38%
split_str_ref    405736/s        30%       28%         27%          11%             4%              2%               1%            --          -37%
split_noalloc    649176/s       108%      105%        103%          78%            66%             63%              62%           60%            --
                     Rate split_str split_regex split_char split_unpack split_char_ref split_unpack_ref split_regex_ref split_str_ref split_noalloc
split_str        318577/s        --         -0%        -1%         -10%           -17%             -21%            -22%          -22%          -52%
split_regex      319956/s        0%          --        -0%         -10%           -17%             -20%            -22%          -22%          -52%
split_char       321554/s        1%          0%         --          -9%           -17%             -20%            -21%          -21%          -51%
split_unpack     353975/s       11%         11%        10%           --            -8%             -12%            -14%          -14%          -47%
split_char_ref   385506/s       21%         20%        20%           9%             --              -4%             -6%           -6%          -42%
split_unpack_ref 401944/s       26%         26%        25%          14%             4%               --             -2%           -2%          -39%
split_regex_ref  409599/s       29%         28%        27%          16%             6%               2%              --           -0%          -38%
split_str_ref    409600/s       29%         28%        27%          16%             6%               2%              0%            --          -38%
split_noalloc    661661/s      108%        107%       106%          87%            72%              65%             62%           62%            --
                     Rate split_char split_str split_regex split_unpack split_regex_ref split_unpack_ref split_char_ref split_str_ref split_noalloc
split_char       314139/s         --       -2%         -3%         -14%            -19%             -21%           -21%          -23%          -52%
split_str        321554/s         2%        --         -1%         -12%            -17%             -19%           -19%          -21%          -51%
split_regex      324588/s         3%        1%          --         -11%            -16%             -18%           -18%          -20%          -50%
split_unpack     364089/s        16%       13%         12%           --             -6%              -8%            -9%          -10%          -44%
split_regex_ref  385506/s        23%       20%         19%           6%              --              -3%            -3%           -5%          -41%
split_unpack_ref 396309/s        26%       23%         22%           9%              3%               --            -0%           -2%          -40%
split_char_ref   398221/s        27%       24%         23%           9%              3%               0%             --           -2%          -39%
split_str_ref    405736/s        29%       26%         25%          11%              5%               2%             2%            --          -38%
split_noalloc    655359/s       109%      104%        102%          80%             70%              65%            65%           62%            --
for, foreach, while

contributeur: Jerome Gouzy

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark qw(cmpthese);
my @A_ = ();
my $MAX   = 100;
$A_[$MAX] = 1;
print scalar @A_."\n";

sub for
{
        my $c=0;
        for (my $i=0;$i<=$MAX;$i++)
        {
                $c++  if ( defined($A_[$i]) );
        }
}

sub foreach
{
        my $c=0;
        foreach my $v (@A_)
        {
                $c++  if ( defined($v) );
        }
}

sub foreach_def
{
        my $c=0;
        @_ = @A_;
        foreach (@_)
        {
                $c++  if ( defined );
        }
}

sub while
{
        my $c=0;
        my $i=0;
        while ($i<=$MAX)
        {
                $c++  if ( defined($A_[$i++]) );
        }
}

for(my $i=0;$i<5;$i++)
{
        cmpthese(
                         -1,
                         {
                          for     => \&for,
                          foreach => \&foreach,
                          while    => \&while,
                          foreach_def => \&foreach_def,
                         }
                         );
}
                Rate     foreach foreach_def       while         for
foreach      37725/s          --        -55%        -57%        -66%
foreach_def  83510/s        121%          --         -4%        -26%
while        87061/s        131%          4%          --        -23%
for         112439/s        198%         35%         29%          --
                Rate     foreach foreach_def       while         for
foreach      37926/s          --        -55%        -56%        -66%
foreach_def  84620/s        123%          --         -3%        -25%
while        87061/s        130%          3%          --        -23%
for         112439/s        196%         33%         29%          --
                Rate     foreach foreach_def       while         for
foreach      38399/s          --        -54%        -56%        -66%
foreach_def  84328/s        120%          --         -3%        -25%
while        87061/s        127%          3%          --        -23%
for         112439/s        193%         33%         29%          --
                Rate     foreach foreach_def       while         for
foreach      38399/s          --        -55%        -56%        -66%
foreach_def  84620/s        120%          --         -3%        -25%
while        87061/s        127%          3%          --        -23%
for         112438/s        193%         33%         29%          --
                Rate     foreach foreach_def       while         for
foreach      38059/s          --        -55%        -56%        -66%
foreach_def  83836/s        120%          --         -4%        -25%
while        87061/s        129%          4%          --        -23%
for         112438/s        195%         34%         29%          --
open, IO::File, backquote

contributeur: Jerome Gouzy

#!/usr/bin/perl

use strict;
use warnings;
use IO::File;

use Benchmark qw(cmpthese);

sub backquote
{
        my @a_ = `cat /etc/hosts`;
}

sub IOFile
{
        my $f_in = new IO::File("/etc/hosts");
        my @a_ = <$f_in>;
        $f_in->close;
}

sub open
{
        my $c=0;
        open(FHOST,"/etc/hosts");
        my @a_ = <FHOST>;
        close(FHOST);
}

for(my $i=0;$i<5;$i++)
{
        cmpthese(
                         -1,
                         {
                          backquote     => \&backquote,
                          open => \&open,
                          IOFile    => \&IOFile
                         }
                         );
}
             Rate backquote    IOFile      open
backquote  5338/s        --      -65%      -72%
IOFile    15459/s      190%        --      -20%
open      19373/s      263%       25%        --
             Rate backquote    IOFile      open
backquote  4370/s        --      -72%      -78%
IOFile    15605/s      257%        --      -20%
open      19549/s      347%       25%        --
             Rate backquote    IOFile      open
backquote  5191/s        --      -67%      -74%
IOFile    15605/s      201%        --      -21%
open      19692/s      279%       26%        --
             Rate backquote    IOFile      open
backquote  5338/s        --      -66%      -73%
IOFile    15605/s      192%        --      -20%
open      19504/s      265%       25%        --
             Rate backquote    IOFile      open
backquote  5144/s        --      -67%      -74%
IOFile    15582/s      203%        --      -21%
open      19692/s      283%       26%        --
chomp

contributeur: Jerome Gouzy

#!/usr/bin/perl

use strict;
use warnings;
use IO::File;

use Benchmark qw(cmpthese);

my @A_HOSTS = `cat /etc/hosts`; 

my @A_ = ();

sub chomp_array
{
        my @a_ = @A_HOSTS;
        chomp(@a_);
        foreach my $l (@a_)
        {
        }
}

sub chomp_array_global
{
        @A_ = @A_HOSTS;
        chomp(@A_);
        foreach my $l (@A_)
        {
        }
}


sub chomp_line
{
        my @a_ = @A_HOSTS;
        foreach my $l (@a_)
        {
                chomp($l);
        }
}

for(my $i=0;$i<5;$i++)
{
        cmpthese(
                         -1,
                         {
                          chomp_array     => \&chomp_array,
                          chomp_array_global => \&chomp_array_global,
                          chomp_line => \&chomp_line,
                         }
                         );
}
                      Rate        chomp_line chomp_array_global      chomp_array
chomp_line         47733/s                --                -6%              -7%
chomp_array_global 50717/s                6%                 --              -1%
chomp_array        51200/s                7%                 1%               --
                      Rate        chomp_line chomp_array_global      chomp_array
chomp_line         47287/s                --                -7%              -7%
chomp_array_global 50716/s                7%                 --              -0%
chomp_array        50717/s                7%                 0%               --
                      Rate        chomp_line      chomp_array chomp_array_global
chomp_line         47287/s                --              -7%                -7%
chomp_array        50716/s                7%               --                 0%
chomp_array_global 50716/s                7%               0%                 --
                      Rate        chomp_line chomp_array_global      chomp_array
chomp_line         47287/s                --                -6%              -7%
chomp_array_global 50242/s                6%                 --              -1%
chomp_array        50716/s                7%                 1%               --
                      Rate        chomp_line      chomp_array chomp_array_global
chomp_line         47287/s                --              -7%                -7%
chomp_array        50716/s                7%               --                 0%
chomp_array_global 50716/s                7%               0%                 --
string contexte

contributeur: Jerome Gouzy

#!/usr/bin/perl

use strict;
use warnings;
use IO::File;

use Benchmark qw(cmpthese);

my $cpt++;

my @A_HOSTS = ();
push(@A_HOSTS, 'x' x 100) while ( $cpt++ < 1000000 );

sub string
{
        my $str='';
        for(my $i=0;$i<$#A_HOSTS;$i+=2)
        {
                $str .= "$A_HOSTS[$i]+$A_HOSTS[$i+1]";
        }
}

# j'en profite pour reverifier scalar vs $#
sub string_scalar
{
        my $str='';
        for(my $i=0;$i<scalar(@A_HOSTS)-1;$i+=2)
        {
                $str .= "$A_HOSTS[$i]+$A_HOSTS[$i+1]";
        }
}

sub nostring
{
        my $str='';
        for(my $i=0;$i<scalar(@A_HOSTS)-1;$i+=2)
        {
                $str .= $A_HOSTS[$i].'+'.$A_HOSTS[$i+1];
        }
}

sub nostring_simple
{
        my $str='';
        for(my $i=0;$i<scalar(@A_HOSTS)-1;$i+=2)
        {
                $str .= $A_HOSTS[$i];
        }
}

sub string_simple
{
        my $str='';
        for(my $i=0;$i<scalar(@A_HOSTS)-1;$i+=2)
        {
                $str .= "$A_HOSTS[$i]";
        }
}

for(my $i=0;$i<5;$i++)
{
        cmpthese(
                         -1,
                         {
                          nostring_simple => \&nostring_simple,
                          string_simple   => \&string_simple,
                         }
                         );
}

for(my $i=0;$i<5;$i++)
{
        cmpthese(
                         -1,
                         {
                          string => \&string,
                          string_scalar => \&string_scalar,
                          nostring => \&nostring,
                         }
                         );
}
                  Rate   string_simple nostring_simple
string_simple   6.73/s              --             -6%
nostring_simple 7.14/s              6%              --
                  Rate   string_simple nostring_simple
string_simple   6.73/s              --             -6%
nostring_simple 7.14/s              6%              --
                  Rate   string_simple nostring_simple
string_simple   6.67/s              --             -7%
nostring_simple 7.14/s              7%              --
                  Rate   string_simple nostring_simple
string_simple   6.73/s              --             -6%
nostring_simple 7.14/s              6%              --
                  Rate   string_simple nostring_simple
string_simple   6.67/s              --             -6%
nostring_simple 7.08/s              6%              --
                Rate        string string_scalar      nostring
string        3.70/s            --           -2%           -6%
string_scalar 3.77/s            2%            --           -4%
nostring      3.92/s            6%            4%            --
                Rate        string      nostring string_scalar
string        3.70/s            --           -2%           -2%
nostring      3.77/s            2%            --            0%
string_scalar 3.77/s            2%            0%            --
                Rate        string string_scalar      nostring
string        3.70/s            --           -2%           -2%
string_scalar 3.77/s            2%            --           -0%
nostring      3.77/s            2%            0%            --
                Rate        string string_scalar      nostring
string        3.70/s            --           -1%           -2%
string_scalar 3.74/s            1%            --           -1%
nostring      3.77/s            2%            1%            --
                Rate        string string_scalar      nostring
string        3.67/s            --           -3%           -3%
string_scalar 3.77/s            3%            --           -0%
nostring      3.77/s            3%            0%            --
benchperl.txt · Last modified: 2011/08/25 14:32 by gouzy