http://qs321.pair.com?node_id=694221


in reply to find closest element of array without going over

"...which just seems wasteful,"
Not as inefficient as you might think. Perl, and the modern processor, is highly optimized for this case. Does your application really require optimal performance here? I think you'd be really hard pressed to code something that was significantly faster. Consider that the whole thing, minus the array, could easily fit into one of the processor's ALU pipelines. The only thing that is going to slow this down is if the array exceeds the L2 cache. That would be how many millions of integers on your machine?

For those whose advocate a binary search, I think that if you were to code it you'd find very little improvement. ( Been there, done that, have the shirt. ) Considering the difficulty in coding a binary search correctly, the likelihood in having bugs therein, and the programmer effort required I would have a hard time recommending such.

s/millions/hundred thousand/


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}