DEV Community

jpt code
jpt code

Posted on

How to measure Time Complexity and Space Complexity?

Before diving into measuring time and space complexity, let's understand asymptotic notation. As described earlier, asymptotic notation helps us simply and compare growth trends without getting lost in small details. Mathematically, it helps us understand how a function changes when it's variable approaches infinity.

Among the three key asymptotic notation, Big O notation is particularly used in describing the upper bound of an algorithm's growth rate, describing the worst-case scenario. In simple term, it tells us the maximum amount of time or space an algorithm might take as input size grows.

In order to measure or evaluate the time complexity of a particular algorithm we need to understand certain concepts. The first and fundamental concept required is looping. Unless and until, we don't understand the looping concept, we cannot measure time complexity on our own and with accuracy. So, we must know the concept of loops. When I am saying this, you should not believe and think whether I am talking about some complex concept or skill. Looping is simply what you have studied in almost all the programming language. We can relate this concept with our real life. Whenever we perform repetitive task under certain conditions and stop after some moment, this is real world looping. This is important because you have to understand the number of times certain operation is done inside algorithm. By knowing this you can sum of the total number of operations.

Similarly, there are few concepts and fundamental to understand when calculating the space complexity of the algorithm. Among them, variable is the most to understand. While saying variable, you should understand variable assignment, variable scope, variable declaration in programming language. If you can understand this, you can easily figure out the space complexity of the algorithm.

In this blog, I have explained the two fundamental concepts to understand before measuring the time and space complexity of the algorithm in very simple way. This will be my first chapter for the series, "how to measure the time and space complexity".

In the coming chapters, I will explain the how looping and variables can impact the most in measuring the time and space complexity.

Top comments (0)