In this article about to discuss how analyze the algorithm and why it is important to do so!!!
Why Analyze Algorithm?
Before that, first we know about what is algorithm?. Algorithm is nothing but it is simply a procedure or formula for solving a problem. Now we have a good question need to answer:
How do analyze algorithms and how can we compare algorithms against each other?
Just imagine there are two friends name as A and B counting the number 0 to 100. A is counting like 0+1+2…. till the end. B is simply make an algorithm n(n+1)/2. Now the difference is like A is calculating the numbers in 100 times, there is the iteration be like 100 times so that the calculation time is also increased. But B is iterate a single time to solve the problem.
Programming from that person A like,
Programming from that person B like,
Just notice both functions have the same result, but completely different algorithms.
How can objectively compare the algorithms?
- We could compare the amount of space they take in memory
- We could also compare how much time it takes each functions to run.
- We can use built in magic function %timeit in Jupyter to compare the time of the function.
- The %timeit magic in Jupyter Notebooks will repeat the loop iteration a certain number of times and take the best result.