array_range_vs_shift.pl 732 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/perl
  2. use 5.014;
  3. use Benchmark qw(cmpthese);
  4. package Foo {
  5. sub new {
  6. bless {}, __PACKAGE__;
  7. }
  8. sub call_me { }
  9. sub bar {
  10. $_[0]->call_me(@_[1 .. $#_]);
  11. }
  12. sub baz {
  13. shift(@_)->call_me(@_);
  14. }
  15. }
  16. my $obj = Foo->new();
  17. cmpthese(
  18. -1,
  19. {
  20. with_shift => sub {
  21. $obj->baz(1, 2, 3, 4, 5);
  22. $obj->baz();
  23. $obj->baz(1);
  24. $obj->baz(1, 2);
  25. },
  26. with_range => sub {
  27. $obj->bar(1, 2, 3, 4, 5);
  28. $obj->bar();
  29. $obj->bar(1);
  30. $obj->bar(1, 2);
  31. },
  32. }
  33. );
  34. __END__
  35. Rate with_range with_shift
  36. with_range 721308/s -- -33%
  37. with_shift 1071850/s 49% --