Skip to content

Instantly share code, notes, and snippets.

@jkeroes
Last active February 3, 2016 23:13
Show Gist options
  • Save jkeroes/f1f40bcc4f4fa7c898eb to your computer and use it in GitHub Desktop.
Save jkeroes/f1f40bcc4f4fa7c898eb to your computer and use it in GitHub Desktop.
benchmarking uniq
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use Benchmark qw(:hireswallclock cmpthese);
use List::MoreUtils qw(uniq);
my @data_sizes = (15_000, 150_000);
# my @data_sizes = (5_000, 20_000);
my $key_count = 10;
my $iters = 100;
sub build_stuff {
my ($size) = @_;
my @stuff;
push @stuff, { map { $_ => 1 } 1..$key_count } for 1..$size;
return @stuff;
}
for my $size (@data_sizes) {
my @stuff = build_stuff($size);
cmpthese($iters, {
"scoped $size uniq" => sub { uniq map { keys %$_ } @stuff },
"unscoped $size uniq" => sub { uniq map keys %$_, @stuff },
"map in hash slice" => sub { my %uniq; @uniq{map keys %$_, @stuff} = () },
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment