Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use Benchmark qw(cmpthese);
  4.  
  5. sub hv {
  6. my ($ref, $key, $default) = @_;
  7. defined $ref && exists $ref->{$key} ? $ref->{$key} : $default;
  8. }
  9.  
  10. my $hash = {
  11. position => 9,
  12. };
  13.  
  14. cmpthese(2_000_000, {
  15. 'Local var' => sub {
  16. if (my $pos = hv($hash, 'position')) {
  17. $pos;
  18. }
  19. },
  20. 'Direct hash access' => sub {
  21. if (hv($hash, 'position')) {
  22. $hash->{position};
  23. }
  24. },
  25. 'Double hv() call' => sub {
  26. if (hv($hash, 'position')) {
  27. hv($hash, 'position');
  28. }
  29. },
  30. });
  31.  
  32. =pod
  33.  
  34. Rate Double vh() call Local var Direct hash access
  35. Double vh() call 1169591/s -- -46% -55%
  36. Local var 2150538/s 84% -- -17%
  37. Direct hash access 2597403/s 122% 21% --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement