Time Complexity Calculator
Free time complexity analyzer for code patterns. Identify complexity from loops, recursion, and common algorithms. Estimate runtime, get optimization suggestions, and understand how your code scales.
Related Calculators
You might also find these calculators useful
Understand Your Code's Performance
Time complexity determines how your code scales. Whether you're preparing for interviews, optimizing production code, or learning algorithms, this calculator helps you analyze code patterns and understand their performance characteristics.
What is Time Complexity?
Time complexity measures how the runtime of an algorithm grows relative to input size. By analyzing code patterns—loops, recursion, and operations—you can predict performance for any input size and identify potential bottlenecks before they become problems.
Time Complexity
T(n) = c · f(n) where c is constant time per operationWhy Analyze Code Patterns?
Predict Performance
Know if your code will handle 1 million records before running it. A O(n²) loop that works for 100 items may timeout at production scale.
Write Better Code
Recognize inefficient patterns like nested loops when hash tables would work. Turn O(n²) into O(n) with the right data structure.
Ace Interviews
Technical interviews focus heavily on complexity analysis. Quickly identify and explain the complexity of any code pattern.
Debug Performance Issues
When code is slow, identify which pattern is the bottleneck. Is it the nested loop? The recursive call? Target your optimization efforts.
How to Analyze Your Code
Common Code Patterns
Single Loop - O(n)
Iterating through an array once: for(i=0; i less than n; i++). Linear growth—doubling input doubles time. Efficient for most use cases.
Nested Loops - O(n²)
Loop within a loop: for(i) for(j). Comparing all pairs, bubble sort. Quadratic growth—use for small n only (<10,000).
Halving Loop - O(log n)
Dividing problem in half: while(n>0) n/=2. Binary search pattern. Extremely efficient—handles billions of elements.
Divide & Conquer - O(n log n)
Split, solve, merge: f(n) = 2f(n/2) + O(n). Merge sort, quicksort average. Optimal for comparison-based sorting.
Frequently Asked Questions
Count nested loops: 1 loop = O(n), 2 nested = O(n²), 3 nested = O(n³). For recursion: linear calls = O(n), binary tree calls = O(2^n), divide-and-conquer with merge = O(n log n). The deepest/most frequent operation dominates.