Skip to content

Instantly share code, notes, and snippets.

@petdance
Created December 3, 2019 19:31
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 petdance/bb4482751f477b64c3d11ff6d143c89d to your computer and use it in GitHub Desktop.
Save petdance/bb4482751f477b64c3d11ff6d143c89d to your computer and use it in GitHub Desktop.
cmp_bag() is slow with large arrays
#!/usr/bin/perl
use warnings;
use strict;
use 5.010;
use Test::More 'no_plan';
use Test::Deep qw( cmp_bag );
use List::Util qw( shuffle );
use Time::HiRes qw( gettimeofday tv_interval );
for my $n ( 1, 10, 100, 1000 ) {
my @stuff = (1..$n);
my @shuffled = shuffle @stuff;
my $cmp_bag = timer( sub { cmp_bag( \@stuff, \@shuffled ) } );
my $deep = timer( sub { is_deeply( [sort @stuff], [sort @shuffled] ) } );
say "$n items";
say "cmp_bag = $cmp_bag";
say "deep = $deep";
}
sub timer {
my $sub = shift;
my $start = [gettimeofday];
$sub->();
return tv_interval( $start );
}
ok 1
ok 2
1 items
cmp_bag = 0.001496
deep = 0.000197
ok 3
ok 4
10 items
cmp_bag = 0.002471
deep = 0.000191
ok 5
ok 6
100 items
cmp_bag = 0.161426
deep = 0.000267
ok 7
ok 8
1000 items
cmp_bag = 14.842759
deep = 0.001545
1..8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment