G
gamo
Recently I see a script nuked by comparison to the C version,
and the two main causes were using bigint, which I don't
really need and 'use integer' could do the job, and don't use
the /gc modifier in a regex instead of a normal /g. I think
that the documentation of the c modifier is not very clear
about its importance. Here is a comparison:
#!/usr/bin/perl -W
use strict;
use Benchmark qw(cmpthese);
my $string = "aabc" x 8192;
my $i;
cmpthese(-3, {
g => sub { while ($string=~/(a+)/g) { $i = $1; } },
gc => sub { while ($string=~/(a+)/gc) { $i=$1; } },
});
__END__
Rate g gc
g 379/s -- -100%
gc 12105006/s 3192107% --
j
and the two main causes were using bigint, which I don't
really need and 'use integer' could do the job, and don't use
the /gc modifier in a regex instead of a normal /g. I think
that the documentation of the c modifier is not very clear
about its importance. Here is a comparison:
#!/usr/bin/perl -W
use strict;
use Benchmark qw(cmpthese);
my $string = "aabc" x 8192;
my $i;
cmpthese(-3, {
g => sub { while ($string=~/(a+)/g) { $i = $1; } },
gc => sub { while ($string=~/(a+)/gc) { $i=$1; } },
});
__END__
Rate g gc
g 379/s -- -100%
gc 12105006/s 3192107% --
j