Skip to content

Instantly share code, notes, and snippets.

@jkeroes
Last active February 4, 2016 00:46
Show Gist options
  • Save jkeroes/ef76af4b97485fb62b5a to your computer and use it in GitHub Desktop.
Save jkeroes/ef76af4b97485fb62b5a to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use Benchmark qw(:hireswallclock cmpthese);
use List::MoreUtils qw(all any);
my @data_sizes = (10_000);
my $key_count = 10;
my $iters = 10_000;
sub build_stuff {
my ($size) = @_;
my @stuff;
push @stuff, { map { $_ => 1 } 1..$key_count } for 1..$size;
return @stuff;
}
sub build_defects {
my @stuff = build_stuff(@_);
my $random_row = int rand(@stuff);
my ($key) = keys $stuff[0]; # the first key is as random as any for our purposes.
delete $stuff[$random_row]->{$key};
return @stuff;
}
for my $size (@data_sizes) {
my @stuff = build_stuff($size);
my @defects = build_defects($size);
cmpthese($iters, {
"all" => sub {
return all { $key_count == keys %$_ } @stuff;
},
"any" => sub {
return any { $key_count != keys %$_ } @stuff;
},
"for" => sub {
for my $row (@stuff) {
next if $key_count == keys %$row;
return 0;
}
return 1;
},
"all vs defects" => sub {
return all { $key_count == keys %$_ } @defects;
},
"any vs defects" => sub {
return any { $key_count != keys %$_ } @defects;
},
"for vs defects" => sub {
for my $row (@defects) {
next if $key_count == keys %$row;
return 0;
}
return 1;
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment