Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Athanasius (Abbot)
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 making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2015-07-04 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls