#!/usr/bin/perl use strict; use warnings; use Benchmark qw/cmpthese/; print "== Single subroutine argument ==\n"; sub b1_shift { my $x = shift; $x . ''; } sub b1_list { my ($x) = @_; $x . ''; } sub b1_direct { $_[0] . ''; } cmpthese( -5, { 'shift' => sub { b1_shift ('X' x rand 5000) }, 'list assignment' => sub { b1_list ('X' x rand 5000) }, 'direct' => sub { b1_direct('X' x rand 5000) } }); print "== Two subroutine arguments ==\n"; sub b2_shift { my $x = shift; my $y = shift; $x . $y; } sub b2_list { my ($x, $y) = @_; $x . $y; } sub b2_direct { $_[0] . $_[1]; } cmpthese( -5, { 'shift' => sub { b2_shift ('X' x rand 5000, 'Y' x rand 5000) }, 'list assignment' => sub { b2_list ('X' x rand 5000, 'Y' x rand 5000) }, 'direct' => sub { b2_direct('X' x rand 5000, 'Y' x rand 5000) } }); __END__ == Single subroutine argument == Benchmark: running direct, list assignment, shift, each for at least 5 CPU seconds... direct: 5 wallclock secs ( 5.10 usr + 0.02 sys = 5.12 CPU) @ 110247.85/s (n=564469) list assignment: 5 wallclock secs ( 5.14 usr + 0.03 sys = 5.17 CPU) @ 89861.90/s (n=464586) shift: 6 wallclock secs ( 5.72 usr + 0.03 sys = 5.75 CPU) @ 70120.00/s (n=403190) Rate shift list assignment direct shift 70120/s -- -22% -36% list assignment 89862/s 28% -- -18% direct 110248/s 57% 23% -- == Two subroutine arguments == Benchmark: running direct, list assignment, shift, each for at least 5 CPU seconds... direct: 5 wallclock secs ( 5.46 usr + 0.00 sys = 5.46 CPU) @ 57434.25/s (n=313591) list assignment: 6 wallclock secs ( 5.30 usr + 0.02 sys = 5.32 CPU) @ 46535.53/s (n=247569) shift: 6 wallclock secs ( 5.00 usr + 0.01 sys = 5.01 CPU) @ 42526.95/s (n=213060) Rate shift list assignment direct shift 42527/s -- -9% -26% list assignment 46536/s 9% -- -19% direct 57434/s 35% 23% --