Empirically estimating big-oh time efficiency
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding Big-Oh Notation
Big-Oh notation is a mathematical representation of the upper bound of an algorithm's time complexity. It characterizes the performance or efficiency of an algorithm in terms of the size of the input data, often denoted by n
. This notation provides a high-level understanding of the algorithm's behavior in the worst-case scenario, often expressed as O(f(n))
where f(n)
is a function that describes how the runtime of the algorithm grows with respect to the input size.
Empirical Estimation of Big-Oh
While theoretical analysis is crucial, empirical estimation provides practical insight into an algorithm’s performance with real-world data. It allows us to validate theoretical assumptions and identify possible deviations.
Steps for Empirical Estimation:
- Select a Representative Set of Inputs: Choose different input sizes to test how the algorithm scales.
- Measure Execution Time: Run the algorithm on these inputs and measure the time taken for each.
- Plot the Results: Create a chart to visualize the relationship between input sizes and execution time.
- Determine the Growth Rate: Analyze the curve and match it to known functions like
n,n^2,log(n),n*log(n), etc. - Refine Your Analysis: Run additional tests as needed to improve accuracy or confirm patterns.
Detailed Example:
Consider a scenario where we want to empirically estimate the time complexity of a sorting algorithm.
Step 1: Select Input
Let’s consider an algorithm that sorts an array. We'll use arrays of varying sizes:
- 1000
- 2000
- 4000
- 8000
- 16000
Step 2: Measure Execution Time
Using Python’s time
module, we can record how long the sorting takes:
- Compare with theoretical predictions.
- Test larger input sizes.
- Analyze additional external factors like system load and cache performance.
- Hardware Variability: Different machines can yield different results due to differences in processors, memory, and other hardware specifics.
- Data Characteristics: The nature of the input data can impact performance, particularly for algorithms optimized for specific data patterns (e.g., nearly-sorted data).
- External Interference: Running tests on shared resources can introduce noise (e.g., other processes competing for CPU time).
Related reading
- enet works but not when run via carettrain
- Ensuring a partially connected digraph is strongly connected
- Enumerating all paths in a directed acyclic graph
- Enumerating all paths in a tree
- Entity Framework async operation takes ten times as long to complete
- Entity Framework Core leaving many connections in sleeping status
- Eppstein's algorithm and Yen's algorithm for k shortest paths
- Epsilon and learning rate decay in epsilon greedy q learning

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.