in reply to Re^2: foreach array - delete current row ?
in thread foreach array - delete current row ?
There is a problem with your two routines that biases the benchmark in their favour.
My routines remove any value containing a 9; yours only keep those containing a 9.
Once you correct that (unless instead of if), you get a different set of numbers:
C:\test>1036622 -N=1e3 Rate grep build_new_array offset_copy for_splice edi +t_in_place grep 678/s -- -22% -27% -34% + -41% build_new_array 870/s 28% -- -6% -15% + -24% offset_copy 929/s 37% 7% -- -9% + -19% for_splice 1024/s 51% 18% 10% -- + -11% edit_in_place 1150/s 70% 32% 24% 12% + -- C:\test>1036622 -N=1e4 Rate for_splice grep offset_copy build_new_array edi +t_in_place for_splice 71.5/s -- -9% -11% -22% + -39% grep 78.7/s 10% -- -2% -14% + -33% offset_copy 80.6/s 13% 2% -- -12% + -31% build_new_array 91.3/s 28% 16% 13% -- + -22% edit_in_place 117/s 64% 49% 46% 29% + -- C:\test>1036622 -N=1e5 Rate for_splice grep build_new_array offset_copy edi +t_in_place for_splice 1.29/s -- -75% -83% -84% + -85% grep 5.12/s 296% -- -33% -35% + -42% build_new_array 7.68/s 494% 50% -- -3% + -14% offset_copy 7.88/s 509% 54% 3% -- + -11% edit_in_place 8.89/s 587% 74% 16% 13% + -- C:\test>1036622 -N=1e6 s/iter for_splice grep build_new_array offset_copy edi +t_in_place for_splice 77.9 -- -98% -98% -99% + -99% grep 1.39 5503% -- -14% -18% + -38% build_new_array 1.20 6379% 16% -- -5% + -28% offset_copy 1.14 6737% 22% 6% -- + -24% edit_in_place 0.868 8884% 60% 39% 31% + --
And actually, my original benchmark was also flawed -- or at least lazy -- in as much as it conflates the time taken to build the original array into the overall timings; which is unrealistic.
Correcting for that
I get yet another set of numbers:
C:\test>1036622 -N=1e3 Rate grep build_new_array offset_copy edit_in_place +for_splice grep 1464/s -- -20% -43% -61% + -65% build_new_array 1820/s 24% -- -29% -52% + -56% offset_copy 2578/s 76% 42% -- -32% + -38% edit_in_place 3803/s 160% 109% 48% -- + -8% for_splice 4144/s 183% 128% 61% 9% + -- C:\test>1036622 -N=1e4 (warning: too few iterations for a reliable count) Rate grep build_new_array offset_copy edit_in_place +for_splice grep 159/s -- -25% -44% -62% + -66% build_new_array 212/s 33% -- -26% -49% + -54% offset_copy 287/s 80% 36% -- -31% + -38% edit_in_place 419/s 163% 98% 46% -- + -10% for_splice 464/s 191% 119% 62% 11% + -- C:\test>1036622 -N=1e5 Rate grep build_new_array offset_copy for_splice edi +t_in_place grep 15.4/s -- -22% -50% -69% + -70% build_new_array 19.8/s 28% -- -36% -60% + -62% offset_copy 31.0/s 102% 57% -- -37% + -40% for_splice 49.6/s 222% 151% 60% -- + -4% edit_in_place 51.6/s 235% 161% 66% 4% + -- C:\test>1036622 -N=1e6 Rate grep build_new_array offset_copy edit_in_place +for_splice grep 1.60/s -- -23% -51% -70% + -71% build_new_array 2.06/s 29% -- -37% -61% + -62% offset_copy 3.28/s 105% 59% -- -38% + -40% edit_in_place 5.26/s 229% 155% 60% -- + -4% for_splice 5.49/s 243% 166% 67% 4% + --
Which just goes to prove a) be careful what you benchmark; b) O(n2) in C can often be considerably faster than O(n) in Perl; if the former avoids the multiple opcodes of the latter.
WHere the copy-offset (or your in-place) really come into their own is when the array being filtered is close to the limits of your memory to start with.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: foreach array - delete current row ? (flaws)
by roboticus (Chancellor) on Jun 03, 2013 at 18:05 UTC | |
|
Re^4: foreach array - delete current row ? (flaws)
by Anonymous Monk on Apr 27, 2015 at 01:55 UTC | |
by BrowserUk (Patriarch) on Apr 27, 2015 at 08:24 UTC |