Skip to content

Instantly share code, notes, and snippets.

@dexterbt1
Last active May 7, 2021 06:23
Show Gist options
  • Save dexterbt1/6380441 to your computer and use it in GitHub Desktop.
Save dexterbt1/6380441 to your computer and use it in GitHub Desktop.
Perl Hash vs Python Dictionary - a micro benchmark we want to benchmark the performance of populating a hash, mapping a fixed-length 10-byte key to the same value
#!/usr/bin/env python
import timeit
REPEATS= 10
using_forloop_total = timeit.timeit(
"""
d={}
for n in range(1000000):
d["%10d" % n] = "%10d" % n
""", number=REPEATS)
print "using_forloop: s/iter = %f" % (using_forloop_total/REPEATS)
# using map(), generate a dict from tuples
using_map_total = timeit.timeit(
"""
d=dict(map(lambda n: ("%10d" % n, "%10d" % n), range(1000000)))
""", number=REPEATS)
print "using_map: s/iter = %f" % (using_map_total/REPEATS)
#!/usr/bin/env perl
use strict;
use Benchmark qw/:all/;
cmpthese(10, {
using_forloop => sub {
my $hash = { };
foreach (1..1_000_000) {
$hash->{sprintf("%10d", $_)} = sprintf("%10d", $_);
}
},
using_map => sub {
# for comparision purposes only, this is very very fast if you
# already have (most of) the data in memory
my $hash = map { sprintf("%10d", $_) => sprintf("%10d", $_) } (1..1_000_000);
},
});
__END__

##Results:

on a MBP 2012 Core i5

Perl 5.12.4

              s/iter using_forloop     using_map
using_forloop   1.73            --          -75%    = ~578k sets/sec
using_map      0.429          303%            --    = ~2.3M sets/sec, inlined ?

Python 2.7.2

using_forloop: s/iter = 1.659030                    = ~602k sets/sec
using_map: s/iter = 1.812623                        = ~551k sets/sec, lambda call overhead ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment