#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use Data::Dump qw[ pp ]; $Data::Dump::WIDTH = 500; our $I //= -1; our $N //= 1000; our @xArrays = map[ map int( rand 2 ), 1 .. 15_000 ], 1 .. $N; our @yArrays = map[ map int( rand 2 ), 1 .. 15_000 ], 1 .. $N; our @xStrings = map{ join '', @$_ } @xArrays; our @yStrings = map{ join '', @$_ } @yArrays; our @xBits = map{ pack 'b*', $_ } @xStrings; our @yBits = map{ pack 'b*', $_ } @yStrings; cmpthese $I, { array => q[ my %top10s; for my $x ( 0 .. $#xArrays ) { for my $y ( 0 .. $#yArrays ) { my $count = 0; $xArrays[$x][$_] == 1 && $yArrays[$y][$_] == 1 and ++$count for 0 .. $#{ $xArrays[ 0 ] }; $top10s{"$x:$y"} = $count; my $discard = ( sort{ $top10s{$a} <=> $top10s{$b} } keys %top10s )[ 0 ]; keys( %top10s ) > 10 and delete $top10s{$discard}; } } $I == 1 and pp ' arrays: ', %top10s; ], strings => q[ my %top10s; for my $x ( 0 .. $#xStrings ) { for my $y ( 0 .. $#yStrings ) { my $count = ( $xStrings[$x] & $yStrings[$y] ) =~ tr[1][]; $top10s{"$x:$y"} = $count; my $discard = ( sort{ $top10s{$a} <=> $top10s{$b} } keys %top10s )[ 0 ]; keys( %top10s ) > 10 and delete $top10s{$discard}; } } $I == 1 and pp 'strings: ', %top10s; ], bits => q[ my %top10s; for my $x ( 0 .. $#xBits ) { for my $y ( 0 .. $#yBits ) { my $count = unpack '%32b*', ( $xBits[$x] & $yBits[$y] ); $top10s{"$x:$y"} = $count; my $discard = ( sort{ $top10s{$a} <=> $top10s{$b} } keys %top10s )[ 0 ]; keys( %top10s ) > 10 and delete $top10s{$discard}; } } $I == 1 and pp ' bits: ', %top10s; ], }; __END__ C:\test>1067218 -I=1 -N=100 (" arrays: ", "44:16", 3911, "23:58", 3913, "78:4", 3907, "54:24", 3909, "10:16", 3929, "78:24", 3928, "23:16", 3920, "23:24", 3922, "58:56", 3917, "54:58", 3914) (warning: too few iterations for a reliable count) (" bits: ", "44:16", 3911, "23:58", 3913, "78:4", 3907, "54:24", 3909, "10:16", 3929, "78:24", 3928, "23:16", 3920, "23:24", 3922, "58:56", 3917, "54:58", 3914) (warning: too few iterations for a reliable count) ("strings: ", "44:16", 3911, "23:58", 3913, "78:4", 3907, "54:24", 3909, "10:16", 3929, "78:24", 3928, "23:16", 3920, "23:24", 3922, "58:56", 3917, "54:58", 3914) (warning: too few iterations for a reliable count) Rate array strings bits array 1.98e-002/s -- -98% -100% strings 1.12/s 5574% -- -82% bits 6.41/s 32272% 471% --