I've recently been pondering the pros and cons of using perl references. Besides all the neat-o things you can do with various scalar/array/hash/sub refs, it occurred to me that I haven't seen any benchmarking to compare perl references versus their standard variable counterparts.
In order to test, I created a simple sort subroutine inside the Benchmark::timeit call. I've bumped the $count up to 2000 to really get some decent feedback. The testfile is simply a 674-line listing of path/filenames from a tarball.
What I found next wasn't particularly exciting. References (while running under strict) were slightly faster than standard variables (if at all). Indeed, occassionally the test results would come back identical for both samples. However, my next example proved quite interesting...
I decided to run the same script, *without* utilizing the strict pragma (yeah, I know, shame on me). Nevertheless, I just wanted to cover all my bases. The samples now put the non-strict test subject at a 10% faster clip than the reference.
I've included all code and a snapshot of the results below. I'm hoping someone can provide more insight as to my findings (as idiotic as they may be) and suggest further test code. I anticipate that I'm probably going to notice great variations in the results based on the type of code I'm benchmarking. Nevertheless, I would like to find a pattern that I can use to determine at what times it's in my best interest to utilize (or ignore) perl references. TIA!
-fp
#!/usr/bin/perl
# benchmark_test_strict.pl
use Benchmark;
use strict;
my @test_array;
open(IN, "testfile");
while (<IN>) {
chomp;
push(@test_array, $_);
}
close(IN);
my $test_ref = \@test_array;
my $t0 = timeit(2000, sub { my @var_results = sort {$a cmp $b} @test_a
+rray; });
my $t1 = timeit(2000, sub { my @ref_results = sort {$a cmp $b} @$test_
+ref; });
my $td = timediff($t0, $t1);
print "\n";
print "normal var (t0)\n";
print "===============\n";
print timestr($t0), "\n\n";
print "reference (t1)\n";
print "==============\n";
print timestr($t1), "\n\n";
print "difference (t0, t1)\n";
print "===================\n";
print timestr($td), "\n\n";
------------------------------------------------------------
[fuzzy@jason fuzzy]$ perl benchmark_test_strict.pl
normal var (t0)
===============
3 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 643.09/s (n=20
+00)
reference (t1)
==============
3 wallclock secs ( 3.10 usr + 0.00 sys = 3.10 CPU) @ 645.16/s (n=20
+00)
difference (t0, t1)
===================
0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU)
------------------------------------------------------------
#!/usr/bin/perl
# benchmark_test.pl
use Benchmark;
open(IN, "testfile");
while (<IN>) {
chomp;
push(@test_array, $_);
}
close(IN);
$test_ref = \@test_array;
$t0 = timeit(2000, sub { @var_results = sort {$a cmp $b} @test_array;
+});
$t1 = timeit(2000, sub { @ref_results = sort {$a cmp $b} @$test_ref; }
+);
$td = timediff($t0, $t1);
print "\n";
print "normal var (t0)\n";
print "===============\n";
print timestr($t0), "\n\n";
print "reference (t1)\n";
print "==============\n";
print timestr($t1), "\n\n";
print "difference (t0, t1)\n";
print "===================\n";
print timestr($td), "\n\n";
------------------------------------------------------------
[fuzzy@jason fuzzy]$ perl benchmark_test.pl
normal var (t0)
===============
4 wallclock secs ( 3.12 usr + 0.00 sys = 3.12 CPU) @ 641.03/s (n=20
+00)
reference (t1)
==============
3 wallclock secs ( 3.50 usr + 0.01 sys = 3.51 CPU) @ 569.80/s (n=20
+00)
difference (t0, t1)
===================
1 wallclock secs (-0.38 usr + -0.01 sys = -0.39 CPU)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.