Re: Find position of element using binary_search
On Mar 10, 8:00 am, Juha Nieminen <nos...@thanks.invalid> wrote:
James Kanze <james.ka...@gmail.com> wrote:
That depends on the application, and the frequency of the
search. In general, the rule is: use std::find until you have a
performance problem. Then evaluate your alternatives. No point
in adding complexity unless you have to.
I have never fully agreed with the "KISS until you have a performance
problem, only then go back and refactor" principle.
It is, never the less, the solution which results in the best
performance, when performance is a problem.
If you know that the program should be prepared for larger amounts of
data (or other similar parameters that will make it need more
calculations), then do it right the first time.
If you know up front that using an O(n) algorithm will cause a
performance problem, then you respond to that performance problem up
front. This is more often an issue when the algorithm is O(n^2),
however.
It's better to do a bit more work now than having to go back
later and having to refactor existing code.
If you've written the code cleanly, the changes will be isolated. If
you have to "refactor" anything, then you were probably paying too
much
attention to performance up front, and not enough to encapsulation.
--
James Kanze