| 1 | package List::Gen::Benchmark; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use List::Gen 0; | ||
| 5 | use Benchmark 'cmpthese'; | ||
| 6 | our ($a, $b); | ||
| 7 | |||
| 8 | print "iteration:\n"; | ||
| 9 | for my $mag (10, 50, map 10**$_, 2 .. 5) { | ||
| 10 | print "\n[+] 0 .. $mag:\n"; | ||
| 11 | my $gen = range $mag; | ||
| 12 | my $sum = $gen->sum; | ||
| 13 | cmpthese -2 => { | ||
| 14 | do => sub { | ||
| 15 | my $i = 0; | ||
| 16 | $gen->do(sub {$i += $_}); | ||
| 17 | $i == $sum or die $i; | ||
| 18 | }, | ||
| 19 | for => sub { | ||
| 20 | my $i = 0; | ||
| 21 | $i += $_ for @$gen; | ||
| 22 | $i == $sum or die $i; | ||
| 23 | }, | ||
| 24 | next => sub { | ||
| 25 | my ($i, $n) = 0; | ||
| 26 | $gen->reset; | ||
| 27 | $i += $n while ($n) = $gen->next; | ||
| 28 | $i == $sum or die $i; | ||
| 29 | }, | ||
| 30 | 'more/next' => sub { | ||
| 31 | my $i = 0; | ||
| 32 | $gen->reset; | ||
| 33 | $i += $gen->next while $gen->more; | ||
| 34 | $i == $sum or die $i; | ||
| 35 | }, | ||
| 36 | iterator => sub { | ||
| 37 | my ($i, $n) = 0; | ||
| 38 | $gen->reset; | ||
| 39 | my $itr = $gen->iterator; | ||
| 40 | $i += $n while ($n) = $itr->(); | ||
| 41 | $i == $sum or die $i; | ||
| 42 | }, | ||
| 43 | '$gen->()' => sub { | ||
| 44 | my ($i, $n) = 0; | ||
| 45 | $gen->reset; | ||
| 46 | $i += $n while ($n) = $gen->(); | ||
| 47 | $i == $sum or die $i; | ||
| 48 | }, | ||
| 49 | reduce => sub { | ||
| 50 | $gen->reduce(sub {$a + $b}) == $sum or die | ||
| 51 | }, | ||
| 52 | handle => sub { | ||
| 53 | my $i = 0; | ||
| 54 | local $_; | ||
| 55 | $gen->reset; | ||
| 56 | $i += $_ while <$gen>; | ||
| 57 | $i == $sum or die $i; | ||
| 58 | }, | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | exit; | ||
| 63 | |||
| 64 | =head1 NAME | ||
| 65 | |||
| 66 | List::Gen::Benchmark - performance tips for using L<List::Gen> | ||
| 67 | |||
| 68 | =head1 SYNOPSIS | ||
| 69 | |||
| 70 | this document contains various benchmarks comparing the parallel syntax styles | ||
| 71 | that L<List::Gen> provides. | ||
| 72 | |||
| 73 | =head1 running the benchmark | ||
| 74 | |||
| 75 | perl -MList::Gen::Benchmark | ||
| 76 | |||
| 77 | =head1 benchmark tests | ||
| 78 | |||
| 79 | iteration: | ||
| 80 | |||
| 81 | reduce: my $x = $gen->reduce(sub {$a + $b}); | ||
| 82 | # same as $gen->sum | ||
| 83 | |||
| 84 | do: my $x; $gen->do(sub {$x += $_}) | ||
| 85 | |||
| 86 | for: my $x; $x += $_ for @$gen; | ||
| 87 | |||
| 88 | more/next: my $x; $gen->reset; | ||
| 89 | $x += $gen->next while $gen->more; | ||
| 90 | |||
| 91 | $gen->(): my ($x, $n); $gen->reset; | ||
| 92 | $x += $n while ($n) = $gen->(); | ||
| 93 | |||
| 94 | iterator: my $it = $gen->iterator; | ||
| 95 | my ($x, $n); $gen->reset; | ||
| 96 | $x += $n while ($n) = $it->(); | ||
| 97 | |||
| 98 | next: my ($x, $n); $gen->reset; | ||
| 99 | $x += $n while ($n) = $gen->next; | ||
| 100 | |||
| 101 | handle: my $x; local $_; $gen->reset; | ||
| 102 | $x += $_ while <$gen>; | ||
| 103 | |||
| 104 | =head1 benchmark results | ||
| 105 | |||
| 106 | iteration: | ||
| 107 | |||
| 108 | [+] 0 .. 10: | ||
| 109 | Rate reduce do for more/next $gen->() iterator next handle | ||
| 110 | reduce 28489/s -- -8% -30% -55% -55% -67% -68% -71% | ||
| 111 | do 31109/s 9% -- -24% -51% -51% -64% -65% -69% | ||
| 112 | for 40877/s 43% 31% -- -35% -35% -53% -55% -59% | ||
| 113 | more/next 62977/s 121% 102% 54% -- -0% -27% -30% -36% | ||
| 114 | $gen->() 63141/s 122% 103% 54% 0% -- -27% -30% -36% | ||
| 115 | iterator 86340/s 203% 178% 111% 37% 37% -- -4% -13% | ||
| 116 | next 90021/s 216% 189% 120% 43% 43% 4% -- -9% | ||
| 117 | handle 98964/s 247% 218% 142% 57% 57% 15% 10% -- | ||
| 118 | |||
| 119 | [+] 0 .. 50: | ||
| 120 | Rate for $gen->() more/next reduce do next iterator handle | ||
| 121 | for 8762/s -- -40% -40% -41% -43% -59% -60% -63% | ||
| 122 | $gen->() 14546/s 66% -- -1% -2% -5% -31% -34% -38% | ||
| 123 | more/next 14646/s 67% 1% -- -2% -4% -31% -33% -38% | ||
| 124 | reduce 14892/s 70% 2% 2% -- -3% -30% -32% -37% | ||
| 125 | do 15278/s 74% 5% 4% 3% -- -28% -30% -35% | ||
| 126 | next 21222/s 142% 46% 45% 43% 39% -- -3% -10% | ||
| 127 | iterator 21892/s 150% 51% 49% 47% 43% 3% -- -7% | ||
| 128 | handle 23454/s 168% 61% 60% 57% 54% 11% 7% -- | ||
| 129 | |||
| 130 | [+] 0 .. 100: | ||
| 131 | Rate for more/next $gen->() reduce do next iterator handle | ||
| 132 | for 4424/s -- -40% -42% -53% -53% -60% -61% -64% | ||
| 133 | more/next 7385/s 67% -- -3% -21% -21% -34% -34% -39% | ||
| 134 | $gen->() 7607/s 72% 3% -- -19% -19% -32% -32% -38% | ||
| 135 | reduce 9340/s 111% 26% 23% -- -0% -16% -17% -23% | ||
| 136 | do 9341/s 111% 26% 23% 0% -- -16% -17% -23% | ||
| 137 | next 11108/s 151% 50% 46% 19% 19% -- -1% -9% | ||
| 138 | iterator 11253/s 154% 52% 48% 20% 20% 1% -- -8% | ||
| 139 | handle 12190/s 176% 65% 60% 31% 31% 10% 8% -- | ||
| 140 | |||
| 141 | [+] 0 .. 1000: | ||
| 142 | Rate for more/next $gen->() next iterator do reduce handle | ||
| 143 | for 446/s -- -39% -42% -61% -62% -62% -63% -64% | ||
| 144 | more/next 735/s 65% -- -4% -35% -37% -37% -39% -41% | ||
| 145 | $gen->() 763/s 71% 4% -- -33% -34% -34% -37% -38% | ||
| 146 | next 1131/s 153% 54% 48% -- -3% -3% -7% -9% | ||
| 147 | iterator 1164/s 161% 58% 52% 3% -- -0% -4% -6% | ||
| 148 | do 1165/s 161% 58% 53% 3% 0% -- -4% -6% | ||
| 149 | reduce 1210/s 171% 65% 58% 7% 4% 4% -- -2% | ||
| 150 | handle 1237/s 177% 68% 62% 9% 6% 6% 2% -- | ||
| 151 | |||
| 152 | [+] 0 .. 10000: | ||
| 153 | Rate for more/next $gen->() next iterator do handle reduce | ||
| 154 | for 44.5/s -- -40% -42% -60% -62% -63% -64% -64% | ||
| 155 | more/next 74.0/s 66% -- -3% -34% -36% -39% -41% -41% | ||
| 156 | $gen->() 76.6/s 72% 4% -- -32% -34% -37% -38% -39% | ||
| 157 | next 113/s 153% 52% 47% -- -3% -8% -9% -10% | ||
| 158 | iterator 116/s 161% 57% 52% 3% -- -5% -7% -7% | ||
| 159 | do 122/s 174% 65% 59% 8% 5% -- -2% -3% | ||
| 160 | handle 124/s 179% 68% 62% 10% 7% 2% -- -1% | ||
| 161 | reduce 125/s 182% 70% 64% 11% 8% 3% 1% -- | ||
| 162 | |||
| 163 | [+] 0 .. 100000: | ||
| 164 | Rate for more/next $gen->() next iterator reduce do handle | ||
| 165 | for 4.47/s -- -41% -42% -61% -62% -63% -64% -64% | ||
| 166 | more/next 7.55/s 69% -- -2% -34% -36% -38% -39% -40% | ||
| 167 | $gen->() 7.71/s 72% 2% -- -32% -35% -36% -38% -39% | ||
| 168 | next 11.4/s 155% 51% 48% -- -4% -6% -8% -9% | ||
| 169 | iterator 11.9/s 166% 57% 54% 4% -- -2% -4% -5% | ||
| 170 | reduce 12.1/s 170% 60% 57% 6% 2% -- -2% -4% | ||
| 171 | do 12.3/s 176% 64% 60% 8% 4% 2% -- -2% | ||
| 172 | handle 12.5/s 180% 66% 63% 10% 6% 4% 2% -- | ||
| 173 | |||
| 174 | =head1 interpretation | ||
| 175 | |||
| 176 | once the generator size gets large enough to mask differences in initialization | ||
| 177 | code, the access methods fall into 4 classes. | ||
| 178 | |||
| 179 | =over 4 | ||
| 180 | |||
| 181 | =item 1: C< for > | ||
| 182 | |||
| 183 | while it is one of the most perlish ways to write the loop, the | ||
| 184 | C< $x += $_ for @$gen > construct uses a tied array, and unfortunately ties are | ||
| 185 | fairly slow. all of the other constructs use the exact same underlying | ||
| 186 | C< FETCH > and C< FETCHSIZE > methods that the tied array uses, yet they are all | ||
| 187 | much faster. | ||
| 188 | |||
| 189 | =item 2: C<< more/next, $gen->() >> | ||
| 190 | |||
| 191 | these are almost the same speed, and are the slowest of the non-tied access | ||
| 192 | methods. the C< more/next > pair results in unneeded function calls, since | ||
| 193 | C< next > is checking the C< more > condition already. the overloaded | ||
| 194 | dereference has some interpreter overhead. | ||
| 195 | |||
| 196 | =item 3: C< next, iterator, reduce, do > | ||
| 197 | |||
| 198 | these are the faster ways to access generators, the ranking of which depends on | ||
| 199 | the size of the generator. C< next > and C< iterator > are almost the same, and | ||
| 200 | are fast across the size range. C< iterator > will always be slightly faster | ||
| 201 | than C< next > since it is the function used internally by C< next> | ||
| 202 | |||
| 203 | C< reduce > and C< do > both have slower start-up time than C< next > or | ||
| 204 | C< iterator > due to a bit of initialization code. this initialization code | ||
| 205 | allows for their loops to be optimized further, which lets C< reduce > and | ||
| 206 | C< do > achieve higher speeds for larger generators. | ||
| 207 | |||
| 208 | =item 4: C< handle > | ||
| 209 | |||
| 210 | handle iteration in a while loop is heavily optimized by perl, and is | ||
| 211 | subsequently one of the fastest techniques for iterating over a generator, and | ||
| 212 | this speed is independent of generator size. | ||
| 213 | |||
| 214 | =back | ||
| 215 | |||
| 216 | in summary, stick to C< next, iterator, reduce, do, or handle > iteration styles | ||
| 217 | if performance is a concern. | ||
| 218 | |||
| 219 | =head1 AUTHOR | ||
| 220 | |||
| 221 | Eric Strom, C<< <asg at cpan.org> >> | ||
| 222 | |||
| 223 | =head1 COPYRIGHT & LICENSE | ||
| 224 | |||
| 225 | copyright 2009-2011 Eric Strom. | ||
| 226 | |||
| 227 | this program is free software; you can redistribute it and/or modify it under | ||
| 228 | the terms of either: the GNU General Public License as published by the Free | ||
| 229 | Software Foundation; or the Artistic License. | ||
| 230 | |||
| 231 | see http://dev.perl.org/licenses/ for more information. | ||
| 232 | |||
| 233 | =cut |