There is a significant time difference between methods 1 and 2 which is presumably because of the inner loop encoded explicitly in perl in method (2) vs the implicit inner loop in method (1) actually implemented in C.
use feature ":5.14";
use warnings FATAL => qw(all);
use strict;
use Time::HiRes qw(time);
use Data::Dump qw(dump pp);
my %checkFrom;
my @toCheck;
sub init0()
{@toCheck = ();
push @toCheck, rand(1e6) for 1..1e4;
}
sub init1()
{%checkFrom = ();
for my $x(0..999)
{for my $y(0..999)
{$checkFrom{$x}[$y] = 1e3*$x+$y;
}
}
init0();
}
sub init2()
{%checkFrom = ();
for my $x(0..999)
{for my $y(0..999)
{$checkFrom{$x}{$y} = 1e3*$x+$y;
}
}
init0();
}
for(1..10)
{if (1)
{init1();
my $s = time();
{my %toCheck = map { $_ => 1 } @toCheck;
@$_ = grep !$toCheck{$_}, @$_ for values %checkFrom;
}
say "1 took ", (time() - $s);
}
if (2)
{init2();
my $s = time();
{for my $thing (@toCheck)
{for my $category (keys %checkFrom)
{delete $checkFrom{$category}{$thing};
}
}
}
say "2 took ", (time() - $s);
}
}
Produces
1 took 1.06206107139587
2 took 11.8416769504547
1 took 1.10106301307678
2 took 12.3792278766632
1 took 1.07640194892883
2 took 12.4490790367126
1 took 1.2350709438324
2 took 12.7647299766541
1 took 1.27407312393188
2 took 12.8677358627319
1 took 1.17906808853149
2 took 12.6504328250885
1 took 1.17000198364258
2 took 12.8158860206604
1 took 1.2890739440918
2 took 12.8577361106873
1 took 1.32807612419128
2 took 12.7377278804779
1 took 1.24107122421265
2 took 12.7136778831482
-
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.
|