segpy icon indicating copy to clipboard operation
segpy copied to clipboard

util.minmax() can be around 25% faster

Open abingham opened this issue 11 years ago • 2 comments

Depending whether util.minmax() figures large in the runtime profile, it may be worth optimizing. The faster algorithm is to take two elements at a time from the input sequence. You first compare them to one another, then compare the smaller to the current minimum and the larger to the current maximum, updating the min/max as appropriate. This amortizes to 3 comparison per 2 input elements rather than the current 4 comparisons. However, it's a somewhat more complex algorithm, so it may not be worth the time.

abingham avatar Sep 02 '14 06:09 abingham

I like that! Is that from TAOCP or somewhere?

rob-smallshire avatar Sep 02 '14 11:09 rob-smallshire

I forget where I picked that up...the fruit of some CS course, probably.

abingham avatar Sep 02 '14 11:09 abingham