Perl never ceases to amaze me. After seeing Arien's point I wondered just how much slower it was to build a stack frame, do the assignment and knock the stack frame down again.
I've seen various references to "the overhead of calling a sub" and have been guilty of not sub-ing when I should have because of it. I needn't have worried. I was expecting a vastly higher overhead than this demonstrates!
#!perl -w
use strict;
use Benchmark;
my @ary = 0 .. 100;
Benchmark::cmpthese ( 10_000_000, {
'assignment' => '$ary[1] = -1;',
'splice' => 'splice @ary, 1, 1, -1;'
}
);
__END__
# Output
C:\test>192779
Benchmark: timing 10000000 iterations of assignment, splice...
assignment: 21 wallclock secs (19.65 usr + 0.00 sys = 19.65 CPU) @ 50
+8931.75/s (n=10000000)
splice: 56 wallclock secs (57.13 usr + 0.01 sys = 57.14 CPU) @ 17
+4999.56/s (n=10000000)
Rate splice assignment
splice 175000/s -- -66%
assignment 508932/s 191% --
What's this about a "crooked mitre"? I'm good at woodwork! |