[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] string increment



On Sun, Apr 23, 2000 at 10:39 PM Arjen Wiersma wrote:
> > > perl -e '@a=qw(a a a a); for($i=$#a;-1 < $i; $i--) {
> > > for($x=0;$x<25;$x++) {$b = ord($a[$i]); $a[$i] = chr(++$b); print
> > > @a; print "\n"; } }'
> Heh, yeah.. I came up with that too, but that increments each character
> with the entire alphabet to each character. If you look at the code I
> wrote it goes it just increments the line, forming the alphabet for
> each character in the line. One by one.
>
> I was wondering if that could be speeded up?
>
> Regards,
>
> Arjen Wiersma
> arjen@wiersma.org

I haven't benchmarked it, but it would seem to me that this would be much
faster:

@alphabet = 'a' .. 'z';
foreach my $one (@alphabet) {
    foreach my $two (@alphabet) {
        foreach my $three (@alphabet) {
            foreach my $four (@alphabet) {
                print join('', $one, $two, $three, $four, "\n");
            }
        }
    }
}

I'm not enough of a Perl expert to optimize that much further, but it takes
under four seconds on my slow architecture to write that to a file. Besides,
he's probably calling crypt() for each combination, and that's his
bottleneck. :)

--
Omer Shenker                     oshenker@iname.com


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe