Skip to content

Instantly share code, notes, and snippets.

@schwern
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schwern/9959154 to your computer and use it in GitHub Desktop.
Save schwern/9959154 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use v5.10;
use strict;
use warnings;
use Benchmark qw(cmpthese);
my @list = (1..1_000_000);
local $, = '';
open my $fh, ">>", "/dev/null";
cmpthese 100, {
list => sub { print $fh @list },
loop => sub { print {$fh} $_ for @list },
join_blank => sub { print {$fh} join '', @list },
join_chr => sub { print {$fh} join 'a', @list },
join_ors => sub { print {$fh} join $,, @list },
join_ors_lexical => sub {
my $ors = $, // '';
print {$fh} join $ors, @list
},
};
__END__
Rate loop list join_chr join_blank join_ors_lexical join_ors
loop 10.5/s -- -49% -58% -74% -75% -75%
list 20.8/s 98% -- -18% -48% -51% -51%
join_chr 25.3/s 141% 22% -- -37% -40% -41%
join_blank 40.2/s 282% 93% 59% -- -5% -6%
join_ors_lexical 42.2/s 301% 103% 67% 5% -- -2%
join_ors 42.9/s 308% 106% 70% 7% 2% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment