Instead of breaking your strings up and then having to iterate the characters, you can use bitwise-string operations to process the characters in parallel and then use tr/// to count the results. This results in a 10x speed up over your original:

#! perl -slw use strict; use Benchmark::Timer; my $T = new Benchmark::Timer; use Math::Random::MT qw[ rand srand ]; our $S ||= 1; our $B ||= 32; our $N ||= 1000; srand( 1 ); my @strings = map { unpack 'b'. $B, rand( 2**32 ) } 1 .. $N; my $label = "$N strings of $B bits (srand:$S)"; $T->start( $label ); for my $i ( 0 .. $#strings ) { for my $j ( $i+1 .. $#strings ) { print join "\t", $i, $j, ( $strings[ $i ] | $strings[ $j ] ) =~ tr[0][0], ## 0 +0 ( ~$strings[ $i ] & $strings[ $j ] ) =~ tr[\1][\1], ## 0 +1 ( $strings[ $i ] & ~$strings[ $j ] ) =~ tr[\1][\1], ## 1 +0 ( $strings[ $i ] & $strings[ $j ] ) =~ tr[1][1]; ## 1 +1 } } $T->stop( $label ); $T->report; __END__ ## Original foreach my $string (@strings) { my @items = split //, $string; $string = \@items; } for ( my $i = 0 ; $i < @strings ; $i++ ) { for ( my $j = $i + 1 ; $j < @strings ; $j++ ) { my ( $c00, $c01, $c10, $c11 ) = ( 0, 0, 0, 0 ); for ( my $k = 0 ; $k < @{ $strings[$i] } ; $k++ ) { $c00++ if ${$strings[$i]}[$k] == 0 && ${$strings[$j]}[$k] == 0; $c01++ if ${$strings[$i]}[$k] == 0 && ${$strings[$j]}[$k] == 1; $c10++ if ${$strings[$i]}[$k] == 1 && ${$strings[$j]}[$k] == 0; $c11++ if ${$strings[$i]}[$k] == 1 && ${$strings[$j]}[$k] == 1; } print join( "\t", $i, $j, $c00, $c01, $c10, $c11 ), "\n"; } } c:\test>627253 >nul 1 trial of 1000 strings of 32 bits (srand:1) (40.336s total) c:\test>627253 >nul 1 trial of 1000 strings of 32 bits (srand:1) (40.477s total) ## Bitwise + tr/// c:\test>627253 >nul 1 trial of 1000 strings of 32 bits (srand:1) (4.711s total) c:\test>627253 >nul 1 trial of 1000 strings of 32 bits (srand:1) (4.696s total)

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Speeding permutation counting by BrowserUk
in thread Speeding permutation counting by albert

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.