We don't bite newbies here... much PerlMonks

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

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

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?

Create A New User
Node Status?
node history
Node Type: note [id://991310]
help
Chatterbox?
 [LanX]: better try 3 [shmem]: that python stuff is none of my business. [shmem]: but sometimes I have to debug things implemeted by "the other team" and stick their nose into their dirt. [shmem]: because it affects "my team" [shmem]: ah well... git is incomplete. There are commands missing: [shmem]: git stop crap [shmem]: for instance

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2018-03-20 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (257 votes). Check out past polls.

Notices?