Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use Benchmark qw(cmpthese);
- sub hv {
- my ($ref, $key, $default) = @_;
- defined $ref && exists $ref->{$key} ? $ref->{$key} : $default;
- }
- my $hash = {
- position => 9,
- };
- cmpthese(2_000_000, {
- 'Local var' => sub {
- if (my $pos = hv($hash, 'position')) {
- $pos;
- }
- },
- 'Direct hash access' => sub {
- if (hv($hash, 'position')) {
- $hash->{position};
- }
- },
- 'Double hv() call' => sub {
- if (hv($hash, 'position')) {
- hv($hash, 'position');
- }
- },
- });
- =pod
- Rate Double vh() call Local var Direct hash access
- Double vh() call 1169591/s -- -46% -55%
- Local var 2150538/s 84% -- -17%
- Direct hash access 2597403/s 122% 21% --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement