Snippets

yujiorama zqKb8n: Untitled snippet

Created by Yuji Okazawa
#!/usr/bin/perl

# https://wimvanderbauwhede.github.io/articles/writing-faster-perl/
# The foreach-loop version takes 2.6 s, the map version 3.3 s, so the map is
# 25% slower. For reference, the index-based for-loop version takes 3.8 s and
# the C-style for-loop version 4.4 s — don't do that! 

use strict;
use warnings;
use utf8;
use feature qw/say/;

use Benchmark qw/:hireswallclock :all/;

sub test_foreach {
    my ($things) = @_;

    my @res;
    foreach (@$things) {
        push @res, 2 * $_ + 1;
    }
    return \@res;
}

sub test_for_idx {
    my ($things) = @_;

    my @res;
    my $number_of_items = scalar(@$things);
    for (my $i=0; $i < $number_of_items; ++$i) {
        $res[$i] = $things->[$i] * 2 + 1
    }
    return \@res;
}

sub test_map {
    my ($things) = @_;

    return [
        map { 2 * $_ + 1 } @$things
    ];
}

sub things {
    my ($number_of_items) = @_;

    return [ map { $_ } (1..$number_of_items) ];
}

sub experiment {
    my ($count, $number_of_items) = @_;

    say "#listsize=${number_of_items}";

    clearallcache();
    my $r = timethese($count, {
        "foreach" => sub { my $x = test_foreach(things($number_of_items)) },
        "for-index" => sub { my $x = test_for_idx(things($number_of_items)) },
        "map" => sub { my $x = test_map(things($number_of_items)) },
    });

    cmpthese($r);
}

experiment(10_000, $_) for (3, 100, 1_000, 10_000);
$ perl wim-vanderbauwhede.pl
#listsize=3
Benchmark: timing 10000 iterations of for-index, foreach, map...
 for-index: 0.0232739 wallclock secs ( 0.02 usr +  0.00 sys =  0.02 CPU) @ 625000.00/s (n=10000)
            (warning: too few iterations for a reliable count)
   foreach: 0.023376 wallclock secs ( 0.02 usr +  0.00 sys =  0.02 CPU) @ 625000.00/s (n=10000)
            (warning: too few iterations for a reliable count)
       map: 0.0108192 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)
            (warning: too few iterations for a reliable count)
                            Rate         for-index           foreach         map
for-index               625000/s                --                0%       -100%
foreach                 625000/s                0%                --       -100%
map       10000000000000000000/s 1600000000000000% 1600000000000000%          --
#listsize=100
Benchmark: timing 10000 iterations of for-index, foreach, map...
 for-index: 0.219677 wallclock secs ( 0.17 usr +  0.00 sys =  0.17 CPU) @ 58139.53/s (n=10000)
            (warning: too few iterations for a reliable count)
   foreach: 0.194073 wallclock secs ( 0.17 usr +  0.00 sys =  0.17 CPU) @ 58479.53/s (n=10000)
            (warning: too few iterations for a reliable count)
       map: 0.145457 wallclock secs ( 0.14 usr +  0.00 sys =  0.14 CPU) @ 71428.57/s (n=10000)
            (warning: too few iterations for a reliable count)
             Rate for-index   foreach       map
for-index 58140/s        --       -1%      -19%
foreach   58480/s        1%        --      -18%
map       71429/s       23%       22%        --
#listsize=1000
Benchmark: timing 10000 iterations of for-index, foreach, map...
 for-index: 1.72424 wallclock secs ( 1.59 usr +  0.00 sys =  1.59 CPU) @ 6277.46/s (n=10000)
   foreach: 1.48833 wallclock secs ( 1.44 usr +  0.00 sys =  1.44 CPU) @ 6958.94/s (n=10000)
       map: 1.74567 wallclock secs ( 1.47 usr +  0.00 sys =  1.47 CPU) @ 6807.35/s (n=10000)
            Rate for-index       map   foreach
for-index 6277/s        --       -8%      -10%
map       6807/s        8%        --       -2%
foreach   6959/s       11%        2%        --
#listsize=10000
Benchmark: timing 10000 iterations of for-index, foreach, map...
 for-index: 22.525 wallclock secs (16.39 usr +  0.03 sys = 16.42 CPU) @ 608.94/s (n=10000)
   foreach: 20.2237 wallclock secs (15.06 usr +  0.00 sys = 15.06 CPU) @ 663.88/s (n=10000)
       map: 18.1616 wallclock secs (16.62 usr +  0.00 sys = 16.62 CPU) @ 601.50/s (n=10000)
           Rate       map for-index   foreach
map       602/s        --       -1%       -9%
for-index 609/s        1%        --       -8%
foreach   664/s       10%        9%        --

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.