Big O from Timing Ratio
Another method that can often be used to find Big O is to look at the ratio of times as the size of input is doubled. Ratios that are approximately powers of 2 are easily recognized:
Ratio | Power of 2 | Big O |
2 | 21 | n |
2+ | 21+ | n * log(n) ? |
4 | 22 | n2 |
8 | 23 | n3 |
Example:
n | Time | Ratio |
4000 | 0.01779 | |
8000 | 0.06901 | 3.8785 |
16000 | 0.27598 | 3.9991 |
32000 | 1.10892 | 4.0180 |
64000 | 4.44222 | 4.0059 |
Since the ratio is about 4, this function is O(n2).