They seem to be about the same.
shift comes out slightly ahead in this test, perhaps because it simplifies the loop.
#!/usr/bin/perl
use warnings;
use strict;
use Benchmark;
timethese(1_000_000, {
'use_shift' => sub { sub_with_shift(0..9) },
'use_list' => sub { sub_with_list(0..9) },
'use_direct' => sub { sub_with_direct(0..9) },
});
sub sub_with_shift
{
my $sum = 0;
while (@_)
{
$sum += shift;
}
$sum;
}
sub sub_with_list
{
my(@a)=@_;
my $sum = 0;
$sum += $_
for @a;
$sum;
}
sub sub_with_direct
{
my $sum = 0;
$sum += $_
for @_;
$sum;
}
Benchmark: timing 1000000 iterations of use_direct, use_list, use_shift...
use_direct: 7 wallclock secs ( 6.48 usr + -0.01 sys = 6.47 CPU) @ 154559.51/s (n=1000000)
use_list: 10 wallclock secs ( 9.85 usr + 0.06 sys = 9.91 CPU) @ 100908.17/s (n=1000000)
use_shift: 6 wallclock secs ( 6.48 usr + 0.01 sys = 6.49 CPU) @ 154083.20/s (n=1000000)
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.