I wonder if you've overlooked my solution, or if there's anything wrong with it.
Not only is it simpler (both less lines of code and conceptually easier to understand, IMHO), it's also consistently faster than what you found to be the fastest:
$ ./846784.pl Rate salva almut salva 881/s -- -15% almut 1037/s 18% --
(tested with v5.10.1, x86_64-linux-thread-multi)
#!/usr/bin/perl use Benchmark "cmpthese"; my $len = shift @ARGV || 12; my $max_reps = shift @ARGV || 2; sub almut { my @set = qw( A B C D E F ); for (1..100) { my $result = ''; while (length($result) < $len) { my $char = $set[ rand @set ]; next if substr($result, -$max_reps) eq $char x $max_reps; $result .= $char; } } } sub salva { my $set = [ qw( A B C D E F ) ]; for (1..100) { my $out = ''; my $last_ix = -1; my $reps = 0; for (1..$len) { my $ix; if ($reps >= $max_reps) { $ix = int rand(@$set - 1); $ix++ if $ix >= $last_ix; } else { $ix = int rand(@$set); } if ($ix == $last_ix) { $reps++; } else { $last_ix = $ix; $reps = 1; } $out .= $set->[$ix]; } } } cmpthese(-1, { almut => \&almut, salva => \&salva, });
In reply to Re^3: Random data generation.
by almut
in thread Random data generation.
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |