Thanks everyone for the help. I tried the benchmark Abigail-II did on
my machine, with a map thrown in for good measure, and got slightly
different results -- strangely, the simple push won!
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw /cmpthese timethese/;
our $size = 100_000;
cmpthese timethese (-10 => {
push => 'my @arr; push @arr => $_ for 0 .. $::size - 1',
map => 'my @arr; @arr = map {$_} (0 .. $::size - 1)',
assign => 'my @arr; $arr [$_] = $_ for 0 .. $::size - 1',
assignpre => 'my @arr; $#arr = $::size - 1;
$arr [$_] = $_ for 0 .. $::size - 1',
slice => 'my @arr; @arr [0 .. $::size - 1] = (0 .. $::size -
+1)',
slicepre => 'my @arr; $#arr = $::size - 1;
@arr [0 .. $::size - 1] = (0 .. $::size -
+1)',
} => 'none');
__END__
Rate map slicepre slice assignpre assign
+ push
map 7.68/s -- -34% -36% -45% -47%
+ -48%
slicepre 11.7/s 52% -- -3% -17% -19%
+ -21%
slice 12.1/s 57% 3% -- -14% -17%
+ -18%
assignpre 14.1/s 83% 20% 16% -- -3%
+ -5%
assign 14.5/s 89% 24% 20% 3% --
+ -2%
push 14.7/s 92% 26% 22% 5% 2%
+ --
Here is what I was running:
This is perl, v5.6.1 built for i386-linux
Linux cchan.acsys.com 2.4.8-26mdk #1 Sun Sep 23 17:06:39 CEST 2001 i68
+6 unknown
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|