Indeed you are correct, my original code is flawed as you describe. Thanks for pointing out my error Cees++
I've re-run a subset of the tests using the following slightly modfied version which corrects this error and the correction slows the insertion time by approximately 1 second for the million keys. The iteration time comes out exactly the same.
print time;
$hash{ substr $_, 0, 4 } .= $/. $_ for 1 .. 1_000_000;
$hash{$_} .= $/ for keys %hash; # Append a final separator
print time;
1069484550
1069484566 # 16 seconds.
$n=0;
print time;
for ( 1 .. 1_000_000 ) {
# Ensure no partial matches by prepending and appending
# separators to the search key.
next unless exists $hash{ substr $_, 0, 4 }
and 1+index( $hash{ substr $_, 0, 4 }, $/.$_.$/ );
$n++;
}
print time, $/, $n;
1069484612
1069484656 # 44 secs.
1000000
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!
-
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.
|