What is the time complexity of my function?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computer science, the time complexity of an algorithm measures the time taken by the algorithm as a function of the length of the input. It provides an upper bound on the growth rate of the running time as a function of the input size, typically denoted as `n`. Understanding the time complexity of a function is crucial in determining its efficiency and scalability.
Analyzing Time Complexity
To determine the time complexity of a function, one needs to analyze the function's operations and identify the most significant component that impacts the running time. Here's how you can approach the analysis:
- Identify Basic Operations: These are operations considered to take a constant amount of time, such as arithmetic operations, variable assignment, or comparison of two values.
- Loop Structures: The time complexity of loops is typically the product of the loop's number of iterations and the complexity of statements within the loop.
- Function Calls: Consider the time complexity of any function calls within the function and how often they are called.
- Recursive Functions: Analyze using a recurrence relation and solve it to find the time complexity.
- Ignoring Constants and Lower Order Terms: Time complexity focuses on the highest order term, and any constant factors or lower order terms are typically omitted as they become insignificant for large input sizes.
Examples of Time Complexity
- Constant Time - : The function's running time does not depend on the input size.
- Linear Time - : The running time grows linearly with the input size.
- Quadratic Time - : The running time increases quadratically with the input size.
- Logarithmic Time - : The running time grows logarithmically as the input size increases.
- Space Complexity: Often analyzed alongside time complexity, it measures the amount of memory an algorithm uses relative to the input size.
- Amortized Time Complexity: In cases like dynamic array expansion, this considers the worst-case over a sequence of operations, smoothing out costly operations.
- Best, Worst, and Average Cases: It's beneficial to analyze the time complexity in different scenarios, especially with algorithms whose performance varies significantly based on input.
Related reading
- What is the time complexity of Ruby's built in permutation and repeated_permutation methods?
- What is the time complexity of the algorithm below?
- What is the time complexity of the following function?
- What is the time complexity of traversing a 2d array
- What is the time complexity of popping an element from a dict in Python?
- What is the use of PYTHONUNBUFFERED in docker file?
- What is the way to understand Proximal Policy Optimization Algorithm in RL?
- What is the worst case for KMP string search algorithm?

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.