Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 2.34 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. use strict;
  2. use warnings;
  3. use Data::Dumper;
  4. use List::Util qw/first shuffle/;
  5. use Benchmark qw(timethese cmpthese);
  6.  
  7. use 5.10.0;
  8.  
  9. my $count = shift || 1;
  10. my $dbg = ($count == 1) ? 1 : undef;
  11.  
  12. my $grep = 'apple';
  13. my @array1 = ('a'..'z', 'A'..'Z', 'apple', 'mango', 'orange');
  14. @array1 = shuffle(@array1);
  15.  
  16. my $comp = timethese(
  17.     $count,
  18.     {
  19.         list1 => sub {list1();},
  20.         list2 => sub {list2();},
  21.         list3 => sub {list3();},
  22.         list4 => sub {list4();},
  23.         list5 => sub {list5();},
  24.     }
  25. );
  26.  
  27.  
  28.  
  29. warn Dumper (\@array1) if $dbg;
  30.  
  31. cmpthese $comp;
  32.  
  33. sub list1 {
  34.     foreach my $r (@array1) {
  35.         if($r eq $grep){
  36.             say "hit1!" if $dbg;
  37.             last;
  38.         }
  39.     }
  40. }
  41.  
  42. sub list2 {
  43.     if(grep /^$grep$/, @array1){
  44.         say "hit2!" if $dbg;
  45.     };
  46. }
  47.  
  48. sub list3 {
  49.     if(grep {$_ eq $grep} @array1){
  50.         say "hit3!" if $dbg;
  51.     };
  52. }
  53.  
  54. sub list4 {
  55.     given ($grep) {
  56.         when (@array1) {
  57.             say "hit4!" if $dbg;
  58.         }
  59.     }
  60. }
  61.  
  62. sub list5 {
  63.     if($grep ~~ \@array1){
  64.         say "hit5!" if $dbg;
  65.     };
  66. }
  67.  
  68. __DATA__
  69. % perl -v
  70.  
  71. This is perl 5, version 14, subversion 1 (v5.14.1) built for darwin-2level
  72.  
  73. Copyright 1987-2011, Larry Wall
  74.  
  75. Perl may be copied only under the terms of either the Artistic License or the
  76. GNU General Public License, which may be found in the Perl 5 source kit.
  77.  
  78. Complete documentation for Perl, including FAQ lists, should be found on
  79. this system using "man perl" or "perldoc perl".  If you have access to the
  80. Internet, point your browser at http://www.perl.org/, the Perl Home Page.
  81.  
  82. % perl list_grep.pl 300000
  83. Benchmark: timing 300000 iterations of list1, list2, list3, list4, list5...
  84.      list1:  1 wallclock secs ( 0.71 usr +  0.01 sys =  0.72 CPU) @ 416666.67/s (n=300000)
  85.      list2:  5 wallclock secs ( 4.95 usr +  0.01 sys =  4.96 CPU) @ 60483.87/s (n=300000)
  86.      list3:  2 wallclock secs ( 1.91 usr +  0.00 sys =  1.91 CPU) @ 157068.06/s (n=300000)
  87.      list4:  1 wallclock secs ( 0.52 usr +  0.00 sys =  0.52 CPU) @ 576923.08/s (n=300000)
  88.      list5:  0 wallclock secs ( 0.45 usr +  0.00 sys =  0.45 CPU) @ 666666.67/s (n=300000)
  89.           Rate list2 list3 list1 list4 list5
  90. list2  60484/s    --  -61%  -85%  -90%  -91%
  91. list3 157068/s  160%    --  -62%  -73%  -76%
  92. list1 416667/s  589%  165%    --  -28%  -38%
  93. list4 576923/s  854%  267%   38%    --  -13%
  94. list5 666667/s 1002%  324%   60%   16%    --