Snippets

yujiorama eqynXy: Untitled snippet

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

# https://www.endpointdev.com/blog/2011/05/benchmarking-perl-map/
# In this case, replacing the imperative programming style here with
# Functional programming (via map) yielded a small performance improvement,
# but the script executed each method 1,000,000 times, so the performance
# gain yielded by just one method call is very small.

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

use Benchmark qw/:hireswallclock :all/;

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

    my @options;

    for my $obj (@$things) {
        push @options, {
            value => $obj->{a},
            label => $obj->{b}
        };
    }
    return \@options;
}

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

    return [ map {
        {
            value => $_->{a},
            label => $_->{b}
        }
    } @$things ];
}

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

    my $base = int(rand(100));

    return [
        map {
            +{ 'a' => $base + 1, 'b' => $base + 2, 'c' => $base + 3 }
        } (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)) },
        "map" => sub { my $x = test_map(things($number_of_items)) },
    });

    cmpthese($r);
}

experiment(10_000, $_) for (3, 100, 1_000, 10_000);
$ perl steph-skardal.pl
#listsize=3
Benchmark: timing 10000 iterations of foreach, map...
   foreach: 0.0579259 wallclock secs ( 0.05 usr +  0.00 sys =  0.05 CPU) @ 212765.96/s (n=10000)
            (warning: too few iterations for a reliable count)
       map: 0.0469069 wallclock secs ( 0.03 usr +  0.00 sys =  0.03 CPU) @ 322580.65/s (n=10000)
            (warning: too few iterations for a reliable count)
            Rate foreach     map
foreach 212766/s      --    -34%
map     322581/s     52%      --
#listsize=100
Benchmark: timing 10000 iterations of foreach, map...
   foreach: 1.04346 wallclock secs ( 1.01 usr +  0.00 sys =  1.01 CPU) @ 9852.22/s (n=10000)
       map: 0.977082 wallclock secs ( 0.92 usr +  0.00 sys =  0.92 CPU) @ 10857.76/s (n=10000)
           Rate foreach     map
foreach  9852/s      --     -9%
map     10858/s     10%      --
#listsize=1000
Benchmark: timing 10000 iterations of foreach, map...
   foreach: 11.135 wallclock secs ( 9.31 usr +  0.00 sys =  9.31 CPU) @ 1073.88/s (n=10000)
       map: 10.9311 wallclock secs ( 9.63 usr +  0.00 sys =  9.63 CPU) @ 1038.96/s (n=10000)
          Rate     map foreach
map     1039/s      --     -3%
foreach 1074/s      3%      --
#listsize=10000
Benchmark: timing 10000 iterations of foreach, map...
   foreach: 152.767 wallclock secs (108.44 usr +  1.19 sys = 109.62 CPU) @ 91.22/s (n=10000)
       map: 138.602 wallclock secs (123.38 usr +  0.45 sys = 123.83 CPU) @ 80.76/s (n=10000)
          Rate     map foreach
map     80.8/s      --    -11%
foreach 91.2/s     13%      --

Comments (1)

  1. Linda Melson
HTTPS SSH

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