time complexity
algorithm analysis
function efficiency
computational complexity
duplicate question

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.

Practice algorithms

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:

  1. 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.
  2. 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.
  3. Function Calls: Consider the time complexity of any function calls within the function and how often they are called.
  4. Recursive Functions: Analyze using a recurrence relation and solve it to find the time complexity.
  5. 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 - O(1)O(1): The function's running time does not depend on the input size.
  • Linear Time - O(n)O(n): The running time grows linearly with the input size.
  • Quadratic Time - O(n2)O(n^2): The running time increases quadratically with the input size.
  • Logarithmic Time - O(logn)O(\log n): 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.