I have nothing to add to liverpole's suggestion, but the way you're doing your benchmark test. There's a Benchmark module available, which is really easy to use. If you write a benchmark code with it once, you'll never write a time calculation again. It is useful to compare results too.
Here is a code that compares liverpole's suggestion and the way you tried it first:
use strict;
use warnings;
use Benchmark qw(:all);
my $limit = 1_000;
sub do_it_tcarmeli {
my %do_it_tcarmeli;
foreach my $i ( 0 .. $limit ) {
$do_it_tcarmeli{$i} = 1;
}
return;
}
my %do_it_liverpole;
sub do_it_liverpole {
foreach my $i ( 0 .. $limit ) {
$do_it_liverpole{$i} = 1;
}
return;
}
cmpthese(
-10,
{
do_it_tcarmeli => \&do_it_tcarmeli,
do_it_liverpole => \&do_it_liverpole,
}
);
Igor 'izut' Sutton
your code, your rules.
-
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.
|