Subject: | C-style loop is slow in unac_string() |
Hi,
I think replacing this:
for (my $offset = 0 ; $offset < $length ; ++ $offset) {
with this:
for my $offset ( 0..$length ) {
will improve the speed
5.10 on windows:
Rate ORIG TEST
ORIG 1208/s -- -28%
TEST 1684/s 39% --
5.8.5 on linux:
Rate ORIG TEST
ORIG 1282/s -- -26%
TEST 1724/s 34% --
Also, I don't think that the function prototype is needed:
sub unac_string ($;$)
Subject: | bench.pl |
#!/usr/bin/perl -w
use strict;
use Benchmark qw(:all);
my $map = {};
my $data = get_data();
cmpthese(1000, {
'ORIG' => \&original,
'TEST' => \&test,
});
die 42 if original() ne test();
sub original {
my $str_in = $data;
my $length = length($str_in);
my $str_out = '';
for (my $offset = 0 ; $offset < $length ; ++ $offset) {
my $chr = substr($str_in, $offset, 1);
$str_out .= exists $map->{$chr} ? $map->{$chr} : $chr;
#print "# <$chr> -> <$map->{$chr}>\n" if exists $map->{$chr};
}
$str_out;
}
sub test {
my $str_in = $data;
my $length = length($str_in);
my $str_out = '';
for my $offset ( 0..$length ) {
my $chr = substr($str_in, $offset, 1);
$str_out .= exists $map->{$chr} ? $map->{$chr} : $chr;
#print "# <$chr> -> <$map->{$chr}>\n" if exists $map->{$chr};
}
$str_out;
}
sub get_data {
open my $FH, '<', $0 or die $!;
local $/;
my $rv = <$FH>;
close $FH;
$rv;
}
Subject: | bench_results.txt |
C:\Documents and Settings\Burak\Desktop>bench.pl
Rate ORIG TEST
ORIG 1208/s -- -28%
TEST 1684/s 39% --
C:\Documents and Settings\Burak\Desktop>perl -v
This is perl, v5.10.0 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)
Copyright 1987-2007, Larry Wall
Binary build 1002 [283697] provided by ActiveState http://www.ActiveState.com
Built Jan 10 2008 11:00:53
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
C:\Documents and Settings\Burak\Desktop>
[burak@dev ~]$ perl bench.pl
Rate ORIG TEST
ORIG 1282/s -- -26%
TEST 1724/s 34% --
[burak@dev03 ~]$ perl -v
This is perl, v5.8.5 built for i686-linux
Copyright 1987-2004, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.
[burak@dev ~]$