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)
In reply to chr() suprisingly slow by sirhc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |