Skip to content

Instantly share code, notes, and snippets.

@chansen
Created April 11, 2011 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chansen/914390 to your computer and use it in GitHub Desktop.
Save chansen/914390 to your computer and use it in GitHub Desktop.
Comparison of hash objects with packed objects (SvPV)
/opt/perl/5.13.11/bin/perl packed_vs_hash.pl
Benchmarking constructor
Rate hash pack
hash 445987/s -- -10%
pack 496264/s 11% --
Benchmarking accessor
Rate vec hash
vec 1190203/s -- -9%
hash 1308227/s 10% --
Benchmarking accessor (list of multiple attributes)
Rate hash unpack
hash 719228/s -- -6%
unpack 762286/s 6% --
/opt/perl/5.12.3/bin/perl packed_vs_hash.pl
Benchmarking constructor
Rate hash pack
hash 437788/s -- -10%
pack 487507/s 11% --
Benchmarking accessor
Rate vec hash
vec 1135525/s -- -4%
hash 1184835/s 4% --
Benchmarking accessor (list of multiple attributes)
Rate hash unpack
hash 675164/s -- -9%
unpack 739499/s 10% --
/opt/perl/5.10.1/bin/perl packed_vs_hash.pl
Benchmarking constructor
Rate hash pack
hash 433194/s -- -8%
pack 468559/s 8% --
Benchmarking accessor
Rate vec hash
vec 1145795/s -- -4%
hash 1189297/s 4% --
Benchmarking accessor (list of multiple attributes)
Rate hash unpack
hash 695712/s -- -6%
unpack 740601/s 6% --
/opt/perl/5.8.9/bin/perl packed_vs_hash.pl
Benchmarking constructor
Rate hash pack
hash 413896/s -- -12%
pack 469801/s 14% --
Benchmarking accessor
Rate vec hash
vec 1105099/s -- -4%
hash 1148337/s 4% --
Benchmarking accessor (list of multiple attributes)
Rate hash unpack
hash 659439/s -- -2%
unpack 675623/s 2% --
/opt/perl/5.6.2/bin/perl packed_vs_hash.pl
Benchmarking constructor
Benchmark: running hash, pack, each for at least 10 CPU seconds...
hash: 11 wallclock secs (10.53 usr + 0.16 sys = 10.69 CPU) @ 442920.95/s (n=4734825)
pack: 11 wallclock secs (10.16 usr + 0.16 sys = 10.32 CPU) @ 525622.29/s (n=5424422)
Rate hash pack
hash 442921/s -- -16%
pack 525622/s 19% --
Benchmarking accessor
Benchmark: running hash, vec, each for at least 10 CPU seconds...
hash: 10 wallclock secs ( 9.95 usr + 0.10 sys = 10.05 CPU) @ 1414538.91/s (n=14216116)
vec: 11 wallclock secs (10.56 usr + 0.18 sys = 10.74 CPU) @ 1293750.65/s (n=13894882)
Rate vec hash
vec 1293751/s -- -9%
hash 1414539/s 9% --
Benchmarking accessor (list of multiple attributes)
Benchmark: running hash, unpack, each for at least 10 CPU seconds...
hash: 11 wallclock secs (10.41 usr + 0.14 sys = 10.55 CPU) @ 753014.12/s (n=7944299)
unpack: 11 wallclock secs (10.17 usr + 0.12 sys = 10.29 CPU) @ 814110.88/s (n=8377201)
Rate hash unpack
hash 753014/s -- -8%
unpack 814111/s 8% --
#!/usr/bin/perl
use strict;
use warnings;
{
package MyVec;
sub new {
my ($class, $y, $m, $d) = @_;
return bless \pack('nCC', $y, $m, $d), $class;
}
sub year {
my ($self) = @_;
return vec($$self, 0, 16);
}
sub month {
my ($self) = @_;
return vec($$self, 2, 8);
}
sub day {
my ($self) = @_;
return vec($$self, 3, 8);
}
sub ymd {
my ($self) = @_;
return unpack('nCC', $$self);
}
}
{
package MyHash;
sub new {
my ($class, $y, $m, $d) = @_;
return bless {
year => $y,
month => $m,
day => $d,
}, $class;
}
sub year {
my ($self) = @_;
return $self->{year};
}
sub month {
my ($self) = @_;
return $self->{month};
}
sub day {
my ($self) = @_;
return $self->{month};
}
sub ymd {
my ($self) = @_;
return @$self{qw(year month day)};
}
}
use Benchmark qw[];
print "Benchmarking constructor\n";
Benchmark::cmpthese( -10, {
'pack' => sub {
my $vobj = MyVec->new(2009, 10, 12);
},
'hash' => sub {
my $hobj = MyHash->new(2009, 10, 12);
},
});
print "\nBenchmarking accessor\n";
my ($vobj, $hobj) = map { $_->new(2009, 10, 12) } qw(MyVec MyHash);
Benchmark::cmpthese( -10, {
'vec' => sub {
my $m = $vobj->month;
},
'hash' => sub {
my $m = $hobj->month;
},
});
print "\nBenchmarking accessor (list of multiple attributes)\n";
Benchmark::cmpthese( -10, {
'unpack' => sub {
my ($y, $m, $d) = $vobj->ymd;
},
'hash' => sub {
my ($y, $m, $d) = $hobj->ymd;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment