Here's a confusing bit of code (below). I'm trying to figure out why it's faster to use split on a string than to pass an anonymous array to a function. (At least that's the case with ActivePerl Build 623.)
Is dereferencing that much of a slowdown?!?
use strict;
use Time::HiRes qw(time);
{
my $mark = time();;
sub mark_time() { $mark = time(); }
sub get_time() {
return time() - $mark;
}
sub show_time() {
print "Time elapsed since last mark: ",
sprintf('%1.4f', get_time() ),
" seconds.\n";
}
}
use constant COUNT => 1000000;
my $Regexp = qr/,/;
sub try_split
{
my ($arg) = @_;
return split $Regexp, $arg;
}
sub try_arrayref
{
my ($arg) = @_;
return @$arg;
}
print "split: ";
mark_time();
for (my $i=0; $i<COUNT; $i++)
{
my ($X, $Y) = try_split 'A,B';
}
show_time();
print "array reference: ";
mark_time();
for (my $i=0; $i<COUNT; $i++)
{
my ($X, $Y) = try_arrayref qw(A B) ;
}
show_time();
In reply to Why is split faster than anonymous arrays? by rrwo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |