I have given run-time functions for two algorithms solving the same problem. Let’s say –
For First algorithm : T(n) = an + b (Linear in n)
For second Algorithm: T(n) = xn^2 + yn + z (Quadratic in n)
Every book says linear in time is better than quadratic and of course it is for bigger n (how big?). I feel definition of Big changes based on the constants a, b, x, y and z.
Could you please let me know how to find the threshold for n when we should switch to algo1 from algo2 and vice-versa (is it found only through experiments?). I would be grateful if someone can explain how it is done in professional software development organizations.
I hope I am able to explain my question if not please let me know.
Thanks in advance for your help.
P.S. – The implementation would be in Java and expected to run on various platforms. I find it extremely hard to estimate the constants a, b, x, y and z mathematically. How do we solve this dilemma in professional software development?
Experiment. I also encountered a situation in which we had code to find a particular instance in a list of instances. The original code did a simple loop, which worked well for several years.
Once, one of our customers logged a performance problem. In his case the list contained several thousands of instances and the lookup was really slow.
The solution of my fellow developer was to add hashing to the list, which indeed solved the customer’s problem. However, now other customers started to complain because they suddenly had a performance problem. It seemed that in most cases, the list only contained a few (around 10) entries, and the hashing was much slower than just looping over the list.
The final solution was to measure the time of both alternatives (looping vs. hashing) and determining the point at which the looping become slower than hashing. In our case this was about 70. So we changed the algorithm:
The solution will probably be similar in your case.