Lably
Skip to content

Time complexity

Estimate of time taken for running an algorithm

  • 15 min read
  • 14 chapters

Related on Lably

Through Algorithm, Big O notation and Function (mathematics)

Since an algorithm's running time may vary among different inputs of the same size, one commonly considers the worst-case time complexity, which is the maximum amount of time required for inputs of a given size. Less common, and usually specified explicitly, is the average-case complexity, which is the average of the time taken on inputs of a given size (this makes sense because there are only a finite number of possible inputs of a given size). In both cases, the time complexity is generally expressed as a function of the size of the input. Since this function is generally difficult to compute exactly, and the running time for small inputs is usually not consequential, one commonly focuses on the behavior of the complexity when the input size increases—that is, the asymptotic behavior of the complexity. Therefore, the time complexity is commonly expressed using big O notation, typically O(n), O(nlog⁡n), O(nα), O(2n), etc., where n is the size in units of bits needed to represent the input.

Algorithmic complexities are classified according to the type of function appearing in the big O notation. For example, an algorithm with time complexity O(n) is a linear time algorithm and an algorithm with time complexity O(nα) for some constant α>0 is a polynomial time algorithm.

Table of common time complexities

The following table summarizes some classes of commonly encountered time complexities. In the table, poly(x)=xO(1), i.e., polynomial in x.

An algorithm is termed constant time (often represented as O(1) time) when its complexity function T(n) is bounded by a value that does not change with the size of the input. This implies that the execution time remains consistent regardless of how much data is processed. For instance, accessing a specific element in an array is a constant-time operation, as it requires only a single operation to locate that element.

In contrast, determining the minimum value in an unordered array is not constant time; it requires examining each element, resulting in linear time complexity, or O(n). However, if the number of elements is known and fixed, certain tasks can still be considered constant time.

Importantly, the term "constant time" does not mean the running time must be entirely independent of the problem size; rather, it should have a consistent upper bound irrespective of input size. For example, a task that involves swapping the values of a and b to ensure a≤b is classified as constant time, even if the execution time may vary depending on whether the condition is already met. The key is that there exists a constant t such that the time taken will never exceed t, regardless of the input values.

Constant-time algorithms are especially significant in contexts like cryptography, where timing attacks can exploit variations in execution time. By designing algorithms that run in constant time, developers can enhance security and ensure performance predictability, making it a fundamental consideration in software engineering.

Sign in to keep reading

13 more chapters are free with a Lably account.

More in Theoretical computer science