Time and Space Complexity
Time Complexity
The running time of an application depends on multiple factors:
Single vs Multiple processes
Read and write speed
32-bit vs 64-bit architecture
Configuration of the machine
Input (How our program behaves with different inputs)
Time complexity is the measure of how the time taken by a program grows with the increase in the number of inputs. We assume that all the other factors remain constant, and we want to calculate how is the speed of the program for input, and how it changes with the size of the input.
Time Complexity Notations
The order or time complexity is calculated through asymptotic assumption. The number of inputs is assumed to be n, and we want to see the upper and lower bound order for the time complexity.
Big O notation:
The notation Ī(n) is the formal way to express the upper bound of an algorithm's running time
It measures the worst-case time complexity or the longest amount of time an algorithm can possibly take to complete.
Omega notation:
The notation Ī©(n) is the formal way to express the lower bound of an algorithm's running time
It measures the best case time complexity or the best amount of time an algorithm can possibly take to complete.
Theta notation:
The notation Īø(n) is the formal way to express both the lower bound and the upper bound of an algorithm's running time
Time complexity Rules
Common asymptotic notations
constant
ā
Ī(1)
logarithmic
ā
Ī(log n)
linear
ā
Ī(n)
n log n
ā
Ī(n log n)
quadratic
ā
Ī(n2)
cubic
ā
Ī(n3)
polynomial
ā
nĪ(1)
exponential
ā
2Ī(n)
Speeding-up API
In micro-service based architecture, we generally try to breakdown every application into small independent modules and create end-points for each micro-service
While speeding up the applications, most of the time, problem does not lie with the iterations.
The optimisation might improve the time by few 0.5 sec or so.
Most number of api calls and connections we are making within the micro-service is the bottleneck
Space Complexity
Space complexity analysis is also almost the same as time complexity analysis
Here, we want to see how efficient a code is in terms of memory usage
Additional space/memory is measured in terms of the largest memory use by the program when it runs. That is to say, if you allocated O(N) memory, and later free it, that does not make the space complexity of your program O(1)
Optimising RAM Space
In general, we don't need to interact with garbage collection for managing the space
Most of the time, declaring few extra objects does not impact the RAM usage as such
However, when multiple instances of a big package is created in different files within an application significantly affects the RAM usage.
Declaring an instance of the big-module globally and using it wherever required within the application significantly helps in lowering down the RAM usage
Recursive Function: Time and Space Complexity
Reference for Further Reading
Last updated
Was this helpful?