Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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??


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?
[Corion]: Mr. Muskrat: I'm not sure if it really happens more often, but I don't exactly know either
[LanX]: yep
[LanX]: more often for some weeks now
[Corion]: I think I'll have to manually (as god) intervene with that node, as the simple reparenting didn't seem to fix the parent/child relationship of the nodes
[Corion]: I think I have an idea but I'll have to open a ticket with Pair.com on that - hopefully I get to that on the weekend
LanX imagines a burning thorn bush
[Mr. Muskrat]: Thank you!
[Mr. Muskrat]: Oh that is odd. I got the message that it was reparented but yeah, it didn't actually do it. lol
[Corion]: LanX: No, Co-Rion appears as Monkey God :-)
[Corion]: Mr. Muskrat: Yeah, that message is automatic when the "normal" reparenting is done, but I think some rows that make up the node were not added to the DB properly

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-01-19 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (170 votes). Check out past polls.