in reply to Re: foreach array - delete current row ?
in thread foreach array - delete current row ?
Update 3: As BrowserUk notes below, my code is flawed. Since he already corrected it, I'll leave my incorrect code here. For future benchmarking, I think I'll have to try and remember to put in a test case to compare the results of all the routines to ensure that they match. I've done that sometimes in the past when I wasn't sure my code was correct. However it seems that I should *always* do it, since I was sure that *this* code was correct. <sigh>
Generally, I just build a new array because it's easiest. I didn't realize that it's reasonably fast, too.
build_new_array=>q[ my @a = 1 .. $N; my @b; for (@a) { push @b, $_ if /9/; } ],
I'm surprised at how much the for_splice version moves around in the rankings.
While waiting for it to finish, I realized that your offset copy method would be faster if you saved the bookkeeping for the end, so I tweaked it a little:
edit_in_place=>q[ my @a = 1 .. $N; my $o = 0; for (@a) { $a[$o++]=$_ if /9/; } $#a=$o-1; ],
$ for J in 2 3 4 5 6; do perl 1036631.pl -N=1e$J; done Rate offset_copy grep for_splice build_new edi +t_in_place offset_copy 3288/s -- -10% -27% -37% + -38% grep 3657/s 11% -- -18% -30% + -31% for_splice 4483/s 36% 23% -- -14% + -16% build_new 5191/s 58% 42% 16% -- + -3% edit_in_place 5338/s 62% 46% 19% 3% + -- Rate offset_copy grep for_splice build_new edi +t_in_place offset_copy 319/s -- -14% -22% -37% + -40% grep 369/s 16% -- -10% -27% + -30% for_splice 411/s 29% 11% -- -19% + -22% build_new 505/s 58% 37% 23% -- + -5% edit_in_place 528/s 66% 43% 29% 5% + -- Rate for_splice offset_copy grep build_new edi +t_in_place for_splice 28.6/s -- -3% -8% -35% + -42% offset_copy 29.4/s 3% -- -5% -33% + -40% grep 31.1/s 9% 6% -- -29% + -37% build_new 43.8/s 53% 49% 41% -- + -11% edit_in_place 49.1/s 72% 67% 58% 12% + -- (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate for_splice offset_copy grep build_new edi +t_in_place for_splice 0.433/s -- -86% -87% -90% + -90% offset_copy 3.00/s 593% -- -11% -28% + -34% grep 3.36/s 676% 12% -- -19% + -26% build_new 4.17/s 862% 39% 24% -- + -8% edit_in_place 4.55/s 950% 52% 35% 9% + -- (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) s/iter for_splice offset_copy grep build_new edi +t_in_place for_splice 632 -- -99% -100% -100% + -100% offset_copy 3.38 18587% -- -18% -28% + -36% grep 2.76 22785% 22% -- -12% + -22% build_new 2.43 25893% 39% 14% -- + -11% edit_in_place 2.16 29142% 56% 28% 13% + --
... will this *ever* finish? If it does before I go to work, I'll update the node with the result.
Update: Added the edit in place chunk, and replaced the output to include those results as well.
Update 2: The 1e6 case finally finished, so I added them. If anyone cares, this is on an Intel Atom 330 1.6GHz machine with 2GB RAM. Additionally, I slightly formatted the output (removing a few blank lines, and inserting some in other places) for readability.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: foreach array - delete current row ? (flaws)
by BrowserUk (Patriarch) on Jun 03, 2013 at 13:44 UTC | |
by roboticus (Chancellor) on Jun 03, 2013 at 18:05 UTC | |
by Anonymous Monk on Apr 27, 2015 at 01:55 UTC | |
by BrowserUk (Patriarch) on Apr 27, 2015 at 08:24 UTC |