The stupid question is the question not asked | |
PerlMonks |
Re: sorting array of arrays referenceby Laurent_R (Canon) |
on Oct 26, 2013 at 21:03 UTC ( [id://1059844]=note: print w/replies, xml ) | Need Help?? |
OK, now I have benchmarked the sort and the walk through only once solution (two variants):
The results show pretty clearly that the walk-through once solution which I originally proposed is much much faster than a sort on the full array:
On a dataset of 2 million records, the walk through once solution is about 21 times faster than the solution sorting the full array. For the walk through once solution, I tried two variants: using sort to maintain the auxiliary array and doing a manual insertion sort on this auxiliary array. It turns out there is very little difference between these two solutions, but the simpler sort function solution is actually slightly better, so it was not worth the effort of trying a manual insertion sort. The larger the dataset, the larger the advantage of the walk through once strategy: with 5 million records, the walk-through once solution is 24.6 faster than the full array sort:
Note that, in order to produce realistic benchmark figures for the sort solution, I had to make a copy of the original array, so that the sort would not work on an already sorted array. Part of the recorded durations is therefore wasted making a copy of the array. If we were to subtract this part from the recorded duration, the advantage of the walk through once solution would be even larger. The copying of the 5-million record array takes about 2 seconds, so that the corrected figures would be 168 sec. for the sort, and 5 sec. for the walk through once solutions, meaning that the later is actually about 33 times faster than the sort.
In Section
Seekers of Perl Wisdom
|
|