Britannica Sources used in this answer

How do computer scientists measure an algorithm’s time and memory needs?

Created from Britannica articles using AI. AI can make mistakes, so verify using Britannica articles.

In computer science, computer scientists measure an algorithm’s efficiency using computational complexity, which evaluates the resources required for an algorithm to function: time complexity (the time required to run) and space complexity (the amount of memory used).123

Because actual resource needs vary based on the size of the input, scientists use mathematical models to predict how these requirements grow as the input size, denoted as n, increases. To describe this growth, they employ big-O notation, which categorizes the order of growth. Common examples include:13

  • Constant time, O(1): The number of operations remains the same regardless of input size.1
  • Linear time, O(n): Resource needs grow proportionally to the input size.12
  • Logarithmic time, O(log n): Needs grow as a logarithm of the input size, often seen in divide-and-conquer approaches.1
  • Quadratic time, O(n²): Needs grow in proportion to the square of the input size.13

Algorithms are often analyzed for best-case, worst-case, and average scenarios to provide a comprehensive understanding of their performance.13