Subject: | Faster implementation without recursion |
Hi! We actually did this same thing at work and then switched to your
module because it's named and documented and whatnot. Eventually I
looked at the source and I thought our impl was better as it has no
recursion. Here's a benchmark of ours being faster as well as our impl.
I'd appreciate it if you included it in your dist :-)
#!perl
use strict;
use warnings;
use Time::HiRes 'gettimeofday';
use Hash::Path;
use feature ':5.10';
sub generate_giant_thing {
my $items = shift;
my $top_level_data_structure = {};
my $current = $top_level_data_structure;
for (0..( $items - 1 )) {
$current->{"f$_"} = {};
$current = $current->{"f$_"};
}
$current->{"f$items"} = 1;
return ($top_level_data_structure, [ map "f$_", (0..$items) ]);
}
my ($foo,$path) = generate_giant_thing(500);
sub our_path {
my $data_set = shift;
my @hash_keys = @_;
my $levels = scalar @hash_keys;
my $return_value = $data_set->{$hash_keys[0]};
for (1..($levels - 1)) {
$return_value = $return_value->{$hash_keys[$_]};
}
return $return_value;
}
{
my $before = gettimeofday;
say our_path($foo, @{$path});
my $after = gettimeofday;
warn 'Our Time: '.sprintf('%0.3f', $after - $before).' seconds';
}
{
my $before = gettimeofday;
say Hash::Path->get($foo, @{$path});
my $after = gettimeofday;
warn 'HP Time: '.sprintf('%0.3f', $after - $before).' seconds';
}