4 ms·
You have no idea what I was doing (or even what language I was using), so don't presume. I benchmarked my binary search, a hand-rolled linear search, and the s
by jallmann 15y ago
You have no idea what I was doing (or even what language I was using), so don't presume.
I benchmarked my binary search, a hand-rolled linear search, and the search function that was built into the container I was using.
binary 0.019003 ms
linear 3.402408 ms
builtin 9.007484 ms
> it's usually better to do a linear search for the sake of having more readable code
For small inputs, maybe. But a binary search takes only marginally more time to test and write than a hand-rolled linear search. And anyone halfway competent should be able to recognize one when reading code. When you have more than a dozen elements or so, a linear search is simply Doing It Wrong.