in reply to Re: Question about speeding a regexp count
in thread Question about speeding a regexp count

You can squeeze out some micro-optimisations. In general, ++$x is slightly faster than $x++; playing with skeeve3_i, I also found that /.../ was faster than /.{3}/ (to an extent that surprised me).

Rate l3epost l3epre l3ipost l3ipre l3epost 42.2/s -- -3% -8% -10% l3epre 43.4/s 3% -- -5% -8% l3ipost 45.8/s 9% 6% -- -3% l3ipre 47.1/s 12% 9% 3% --
is the output from this code:
use strict; use Benchmark qw/ cmpthese /; our $a = "abcdefghij" x 1000; cmpthese(-1, { l3epost => q{ my %a; $a{$1}++ while $a =~ /(?=(.{3}))/g }, l3epre => q{ my %a; ++$a{$1} while $a =~ /(?=(.{3}))/g }, l3ipost => q{ my %a; $a{$1}++ while $a =~ /(?=(...))/g }, l3ipre => q{ my %a; ++$a{$1} while $a =~ /(?=(...))/g }, });

It shouldn't be hard to improve perl to compile $x++ to a preincrement in void context; fixing the regexp engine to make an explicit fixed count as fast as unrolling is likely to be harder.

Update: Now I'm confused: I checked the source, and void postincrement should be getting replaced with preincrement at compile time; I definitely don't understand now why explicit preinc is showing consistently faster times.

Hugo