use strict; use warnings; use Benchmark qw(:all); use constant COUNT => 200000; my($str, $tmp, $len); sub in1 ($) { $tmp = length($_[0]); } sub in2 ($) { $tmp = length(${$_[0]}); } sub out1 () { return($str); } sub out2 () { return(\$str); } sub fun1 ($) { my $new = $_[0] . "x"; return($new); } sub fun2 ($) { my $new = ${ $_[0] } . "x"; return(\$new); } foreach $len (1, 10, 100) { $str = "A" x ($len * 1000); timethese(10, { "in1x$len" => sub { for (1 .. COUNT) { in1($str) } }, "in2x$len" => sub { for (1 .. COUNT) { in2(\$str) } }, "out1x$len" => sub { for (1 .. COUNT) { $tmp = out1() } }, "out2x$len" => sub { for (1 .. COUNT) { $tmp = out2() } }, "fun1x$len" => sub { for (1 .. COUNT) { $tmp = fun1($str) } }, "fun2x$len" => sub { for (1 .. COUNT) { $tmp = fun2(\$str) } }, }); }