in reply to Perl at Rosetta Code, with one particular example

Two things (so far).
Slurping a file and regexing out what you want is faster than reading the file line by line in a while loop (in my experience).
Hash slices are faster than separate lookups (ditto).

My general rule for speed is to try to do as much as you can in the perl interpreter, as opposed to the perl language. See Re: converting binary to decimal as an example. Try to do operations on groups of things, not the individual elements.

Recheck the RosettaCode page :)

  • Comment on Re: Perl at Rosetta Code, with one particular example

Replies are listed 'Best First'.
Re^2: Perl at Rosetta Code, with one particular example
by Anonymous Monk on Jun 12, 2025 at 06:41 UTC

    Thanks for fixing the RC; it's slower; + RAM now at ~13.5 Mb vs. mine of ~10 Mb according to my crude measurements.

    use strict; use warnings; use Benchmark 'cmpthese'; cmpthese -3, { am => sub { my ( @a, %h ); open my $fh, '<:raw', 'unixdict.txt' or die; while ( <$fh> ) { next unless length > 6; chomp; ( -1 != index $_, 'e' ) ? ( push @a, $_ ) : ( -1 != index $_, 'i' ) ? ( $h{ $_ } = 1 ) : 1 } close $fh; my ( @ret, $i ); exists $h{ $i = tr/e/i/r } and push @ret, sprintf "%30s %s\n", $_, $i for @a; @ret }, rc => sub { my $file = do { local (@ARGV, $/) = 'unixdict.txt'; <> }; my %i = map { tr/e/i/r => sprintf "%30s %s\n", $_, tr/e/i/r } $file =~ /^(?=.{6}).*e.*$/gm; @i{ split ' ', $file }; } } __END__ Rate rc am rc 25.2/s -- -65% am 71.1/s 182% --