Wondering if perl4 would run one of my RegExp-happy scripts faster than perl5 I had to roll my own chr() and thought I'd benchmark it against the builtin.
I was surprised to see that the chr() builtin was significantly slower than pack("C"). Even more surprised that the sub mychr was faster than the direct pack("C"). Can anyone explain why either of these is the case?
% perl5.8.0 use Benchmark; sub mychr { return pack("C", shift); } @array = @array = (0 .. 255) x 100; timethese(1000, { 'sub' => 'foreach (@array) { my $chr = chr($_) }', 'builtin' => 'foreach (@array) { my $chr = &mychr($_) }', 'direct' => 'foreach (@array) { my $chr = pack("C", $_) }', }); Benchmark: timing 1000 iterations of builtin, direct, sub... builtin: 156 wallclock secs (143.82 usr + 0.17 sys = 143.99 CPU) @ + 6.94/s (n=1000) direct: 53 wallclock secs (48.89 usr + 0.04 sys = 48.93 CPU) @ 20 +.44/s (n=1000) sub: 37 wallclock secs (31.16 usr + 0.03 sys = 31.19 CPU) @ 32 +.06/s (n=1000)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: chr() suprisingly slow
by Abigail-II (Bishop) on Jul 30, 2002 at 14:21 UTC | |
by sirhc (Initiate) on Jul 30, 2002 at 16:09 UTC | |
by trs80 (Priest) on Aug 04, 2002 at 08:51 UTC | |
|
Re: chr() suprisingly slow
by demerphq (Chancellor) on Jul 30, 2002 at 14:39 UTC |