by defrost · 3y ago · 0 comments
Many people ragging on the founder|coder for writting a clean snippet of linear search function when he obviously should have done better and used a binary search (or some other algo) .. ergo "Bad Code!!".
What was missing from that was any context about the volume of expected data per "need to seach this" call etc.
It's entirely possible that every instance was to quickly search an already primed cache of data and that the optimal performance on the target hardware was to minimise branching and linearly rip through a small chunk of data in a load page.
Or, maybe not.
Point being, context and bigger picture plays a part here also.
https://stackoverflow.com/questions/65753731/why-searching-a...
Hardly clean, I think you'd agree.
In any case, you shouldn't use explicit iteration to solve this problem without a good reason. Every language should have both linear and binary search as part of its standard library.
I agree that you shouldn't especially choose one algorithm over another without reason and I feel comfortable choosing a linear scan of short sections of cached data in pipelined situations where branching overheads are steep.
Was the case in the TV episode such a scenario? I have no knowledge of that myself.
Vouched.