A
Adrien BARREAU
Hello.
Here is a piece of code:
---
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes;
use Benchmark;
my @strings = map { sprintf "%08X\n", rand(0xffffffff); } 1 .. 100;
my $r = qr/some/;
sub compiled
{
/$r/ for (@strings)
}
sub live
{
/some/ for (@strings)
}
my $results = Benchmark::timethese(100000, {
'compiled' => \&compiled,
'live' => \&live,
});
Benchmark::cmpthese($results);
---
Running it gives me:
Benchmark: timing 100000 iterations of compiled, live...
compiled: 2 wallclock secs ( 2.67 usr + 0.00 sys = 2.67 CPU) @
37453.18/s (n=100000)
live: 1 wallclock secs ( 0.90 usr + 0.00 sys = 0.90 CPU) @
111111.11/s (n=100000)
Rate compiled live
compiled 37453/s -- -66%
live 111111/s 197% --
On: This is perl 5, version 14, subversion 2 (v5.14.2) built for
x86_64-linux-gnu-thread-multi
I don't really understand these results: qr// seems to cost more, but I
don't find anything in the perldoc about that.
Do I miss an error in this benchmark?
Does somebody have any information about that overhead I see?
If I had to guess, I would suspect dereferencing cost of a Regexp ref.
Could it be right?
Adrien.
Here is a piece of code:
---
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes;
use Benchmark;
my @strings = map { sprintf "%08X\n", rand(0xffffffff); } 1 .. 100;
my $r = qr/some/;
sub compiled
{
/$r/ for (@strings)
}
sub live
{
/some/ for (@strings)
}
my $results = Benchmark::timethese(100000, {
'compiled' => \&compiled,
'live' => \&live,
});
Benchmark::cmpthese($results);
---
Running it gives me:
Benchmark: timing 100000 iterations of compiled, live...
compiled: 2 wallclock secs ( 2.67 usr + 0.00 sys = 2.67 CPU) @
37453.18/s (n=100000)
live: 1 wallclock secs ( 0.90 usr + 0.00 sys = 0.90 CPU) @
111111.11/s (n=100000)
Rate compiled live
compiled 37453/s -- -66%
live 111111/s 197% --
On: This is perl 5, version 14, subversion 2 (v5.14.2) built for
x86_64-linux-gnu-thread-multi
I don't really understand these results: qr// seems to cost more, but I
don't find anything in the perldoc about that.
Do I miss an error in this benchmark?
Does somebody have any information about that overhead I see?
If I had to guess, I would suspect dereferencing cost of a Regexp ref.
Could it be right?
Adrien.