Big-O Estimator from Timings
Time your code on a few input sizes, paste the numbers, and see which growth rate fits best.
How it works
Time your code at several input sizes and paste size and time pairs. For each growth rate (n, n log n, n squared and so on) the tool divides every time by that rate. If the growth rate is right, all the results are about the same constant. The one with the smallest spread wins.
The formula
for a model f(n): ratio = time / f(n) misfit = spread of log(ratio) log-log slope of time against n: 1 is linear, 2 is quadratic
Worked example
Times 0.8, 3.1, 12.6 and 50 for n = 1000, 2000, 4000 and 8000: each doubling multiplies the time by about 4, so it fits O(n^2).
Good to know
- Double the size each time. A doubling that multiplies time by 2 means linear, by 4 quadratic, by 8 cubic.
- Use big sizes. Small ones are dominated by start-up cost and noise.
- Run each size several times and use the median.
Common questions
- Can this prove a complexity?
- No. It only fits measured data. A proof comes from analysing the code.
- Why does n log n look like n?
- Over a small range log n changes slowly. Use a wide range of sizes.
- Does the unit of time matter?
- No. Seconds, milliseconds and ticks all work.
More in CS Labs & Coding.
Watch 10 sorting algorithms animate step by step with live counters and pseudocode, or race them on the same input.
CS Labs & CodingTOP PICKGraph Algorithm VisualizerTOP PICKDraw your own graph and watch BFS, DFS, Dijkstra, Bellman-Ford, Prim, Kruskal, topological sort and more run step by step, with the queue, the distance table and the reason for every move.
Dynamic Programming VisualizerWatch the DP table fill cell by cell for LCS, edit distance, knapsack, LIS, coin change and matrix chain, with the recurrence and real numbers for each cell, then the traceback to the answer.
OPEN →