Re:Why is it faster to process a sorted array than an unsort...
by staticassertion on Friday September 15, 2017 @10:06AM
(#15257224)
I wouldn't expect the cost to offset the impact of having to sort first. But, if you need to process the same data many times, it may be a good idea to sort it up front.
[ Parent
| on HN ]
Re:Why is it faster to process a sorted array than an unsort...
by pletnes on Friday September 15, 2017 @10:10AM
(#15257253)
In that case, loop fusion may be even more efficient - makes better use of CPU caches. Depends on the logic I suppose.
Re:Why is it faster to process a sorted array than an unsort...
by staticassertion on Friday September 15, 2017 @10:13AM
(#15257276)
That's the thing, it's feels impossible to generalize since this can get pretty arbitrary. The size of the data, how sorted the data already is, what you're processing, etc.
Re:Why is it faster to process a sorted array than an unsort...
by redcalx on Friday September 15, 2017 @11:27AM
(#15257916)
Yes heuristic logic can have a role here, e.g. if list has length M and number of passes through it M, do N*M and if the result is over some threshold then make the effort of sorting the list. But a good value for that threshold depends on many factors, hence a heuristic may make sense in some scenarios. It's the same sort of pattern used with SQL query optimizers, e.g. cardinality of a result set is one of the key factors that influence how some query on that result set will be executed, e.g. whether to scan versus use an index.