Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Is it possible to generate all possible combinations of 2-letter and 3-letter words in perl?

by Athanasius (Monsignor)
on Sep 02, 2012 at 17:31 UTC ( #991310=note: print w/ replies, xml ) Need Help??


in reply to Is it possible to generate all possible combinations of 2-letter and 3-letter words in perl?

Although glob performs well for small word sizes, for larger sizes a hand-rolled loop is much faster:

#! perl use strict; use warnings; use Benchmark qw(cmpthese); cmpthese(1, { 'loop' => sub { my @p = permute_loop(10); print 'loop: ', scalar @ +p, "\n"; }, 'glob' => sub { my @q = permute_glob(10); print 'glob: ', scalar @ +q, "\n"; }, }); sub permute_glob { my ($size) = @_; my $r = 'A,T,G,C'; return glob "{$r}" x $size; } sub permute_loop { my ($size) = @_; my @a = qw(A T G C); while (--$size) { @a = map { $_ . 'A', $_ . 'T', $_ . 'G', $_ . 'C' } @a; } return @a; }

Output:

glob: 1048576 (warning: too few iterations for a reliable count) loop: 1048576 (warning: too few iterations for a reliable count) s/iter glob loop glob 281 -- -98% loop 4.60 6011% --

Hope that helps,

Athanasius <°(((><contra mundum


Comment on Re: Is it possible to generate all possible combinations of 2-letter and 3-letter words in perl?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991310]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-12-22 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (112 votes), past polls