Introduction
In this article, we will be talking about what performance of an algorithm is and how Complexity is used to measure performance, and also learn what Big-O notation is.
Background
When we write a program or an algorithm that will be used only once on a small amount of data and then discarded, we focus more on writing the code (program) in the easiest possible way we know, we debug it, test it and then use it in our application and then forget about it. However, when we need to write a program or an algorithm that will be used over a longer period of time and will be maintained by many people, we need to focus on factors like simplicity, clarity, and efficiency /performance of the program or algorithm. In this article, we will be focusing on the efficiency/performance of an algorithm. How is it measured? What is the complexity of the algorithm? We will also discuss Big-O Notation, which is a unit for expressing complexity.
So let’s start with Performance and see how performance is measured.
Performance of a program or an algorithm is measured using various factors, such as Time, Space, and Network.
- Time: How much time does the program take to execute?
- Space: How much memory or space the program is using while it's executed (also the physical space)
- Network: if there are any network-related activities the program is doing, which may affect the performance.
Of all the factors mentioned above, Time are Space are usually considered important when measuring the performance of an algorithm. Example – How fast is your algorithm compared to another algorithm? How much space does your algorithm take in terms of memory?. When we talk about the time taken by the algorithm, we are actually talking about Complexity (Time Complexity). “The complexity of an algorithm is high when the algorithm takes more time to execute,” and more time means your algorithm has low performance.
Let’s discuss more about Complexity and understand what Time complexity is.
Definition
Complexity is used to measure performance. Or, Complexity is a measure of how the Resources (in this case, Time) change as the size of the problem gets larger.
Let’s understand this with an example: For small input of data, an algorithm might run fast and might not show any time difference, but as the input size increases, the program might take time to execute and become slow, and performance is low; this is where the complexity is measured. The Complexity of an algorithm indicates to us how performant the algorithm is.
So we can say that - Size of input determines complexity, which in turn determines how the algorithm behaves when the input size is very large. In computer terms, this is also called the worst-case performance of the algorithm.
Now that we know what complexity is, we can see how complexity is measured. This is done using Big–O notation. Let’s take an example.
Example
Suppose you have two algorithms and you want to compare them based on the performance, i.e., Time complexity. How do you do that?
- One way is - You can practically run each of them for the same inputs and wait till they execute, and note the time difference. In some cases, this might not be feasible to practically run each algorithm, taking into consideration the time and resources required to execute them. In such a case, the other way is to prove this theoretically using mathematical terms – that is, using the Big–O notation.
- Big–O notation is used to study the performance/complexity of an algorithm in theoretical terms. Big–O notation looks at the upper bound of an algorithm's performance; i.e., its worst-case behavior. Big–O notation also looks at algorithms' asymptotic behavior – what it means is the performance of the algorithm as the size of the input increases to very large. The size of the input is represented by n.
Definition
The Big–O notation is a unit to express complexity in terms of the size of the input that goes into an algorithm.
The following are the Big–O notation rules to figure out an algorithm's performance or asymptotic behavior,
Constant Time Complexity O(1)
If the time taken by the algorithm does not change and remains constant as the input size is increased, then the algorithm is said to have a complexity of O(1) – read as Order of 1, performance. The algorithm doesn’t depend on the size of the input. Example – Node added at the end of the linked list (assuming we have a tail pointer).
static void ConstantTimeComplexity(int n)
{
int a = 100;
int b = 200;
int sum = a + n;
int mul = b * n;
Console.WriteLine("{0}, {1}", sum, mul);
}
O(log n)
An algorithm for input size n (where n is very large) breaks the problem into small chunks for each iteration. The algorithm is said to have a complexity of O (log n) - read as Order log n performance. Example: Binary Search Tree.
Linear Time Complexity O(n)
An algorithm for input size n (where n is very large) performs n steps, and the time taken by the algorithm changes linearly as the input size increases; then the algorithm is said to have complexity of O(n) – read as Order of n performance. The run time complexity is proportional to n.
Example
For loop to the size of the length of the array, as the array size increases, the time taken will proportionately increase.
static void LinearTimeComplexity(int[] arr1)
{
for (int i = 0; i < arr1.Length; i++)
{
Console.WriteLine("{0}", arr1[i]);
}
}
O (n log n)
An algorithm for input size n (where n is very large), breaks the problem into small chunks for each invocation, and then takes each of the smallest chunks and stiches them back together, then the algorithm is said to have complexity of O(n log n) - read as Order n log n performance. Example: Quick Sort.
Quadratic: O(n2)
An algorithm for input size n (where n is very large) performs almost double (n2) the steps, and the time taken by the algorithm changes quadratically as the input size increases. The algorithm is said to have a complexity of O(n2) – read as Order n square performance. Example bubble Sort.
static void QuadraticTimeComplexity(int[] arr1)
{
for (int i = 0; i < arr1.Length; i++)
{
for (int j = 0; j < arr1.Length; j++)
{
Console.WriteLine("{0}", arr1[i] + arr1[j]);
}
}
}
- Cubic: O (n3)
- Very rare algorithms.
- Exponential
- O (n!) – Incredibly rare.




Upendra Pratap ShahiPosted Aug 29, 2017, 1:02 PM
Nice one share....keep it up..
Arjan de HaanPosted Aug 29, 2017, 4:27 AM
Isn't your complexity for example 4 wrong?You state that because of the nested loops complexity is O(n2), but in example 2 (the note) you say that it is _not_ O(n2) because they are 2 different inputs? Shouldn't the complexity of example 4 be O(ab) + O(a)?