#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); our $S ||= 1; our $B ||= 32; our $N ||= 100; srand( 1 ); our @strings = map { unpack 'b'. $B, rand( 2**32 ) } 1 .. $N; my $bitwise = <<'EOBW'; our @strings; my ($c00, $c01, $c10, $c11) = (0, 0, 0, 0); for my $i ( 0 .. $#strings ) { for my $j ( $i+1 .. $#strings ) { $c00 = ( $strings[ $i ] | $strings[ $j ] ) =~ tr[0][0]; $c01 = ( ~$strings[ $i ] & $strings[ $j ] ) =~ tr[\1][\1]; $c10 = ( $strings[ $i ] & ~$strings[ $j ] ) =~ tr[\1][\1]; $c11 = ( $strings[ $i ] & $strings[ $j ] ) =~ tr[1][1]; } } EOBW my $ifchop = <<'EOIFC'; our @strings; my ($c00, $c01, $c10, $c11) = (0, 0, 0, 0); for my $i ( 0 .. $#strings ) { for my $j ( $i+1 .. $#strings ) { # Need copies because chop is destructive # Oops... my $test_str_1 = $i; # my $test_str_2 = $j; my $test_str_1 = $strings[$i]; my $test_str_2 = $strings[$j]; while ($test_str_1) { my $x = chop $test_str_1; my $y = chop $test_str_2; if ($x) { if ($y) { $c11++; } else { $c10++; } } else { if ($y) { $c01++; } else { $c00++; } } } } } EOIFC cmpthese(100, { bitwise => $bitwise, ifchop => $ifchop, });