Time Complexity
Hello people, in this blog we are going to read about the “Time
Complexity”.
What is time complexity? What is the importance of time
Complexity?
In programming, every
code is like a solution to some problem, and there can be number of solutions
to such problems. So for picking the best of all solutions we work on finding
the fastest solution, here the fastest solution is considered to be the best
solution. So for having the best solution of our problems, we must have a good
knowledge of time complexity.
According to Wikipedia, “Time complexity is the computational
complexity that describes the amount of time
it takes to run an algorithm.”
A good thing about time complexity is that it
doesn’t bothers about the configuration of the machine we are using, it only
depends on the time taken by the code with respect to the input we have
provided.
Now the question arrives “How do we calculate the time taken by any code to run?”, So explaining
about calculation of running time of a code, we start by letting an
hypothetical set of rules
Rule 1 – It takes
1 unit of time for any logical or algorithmic operation.
Rule 2 – It takes 2 unit of time for
assignment and return
Now let us calculate the time taken by the following
code on the basis of above mentioned rules;
So the total time taken by this code to run
will be 3 units.
Now let’s take another example on the same set
of rules;
So the total time taken by this code to run
will be [ 2 + 4(n+1) + 3n + 2 ]units.
We can now say that
TSum
= 3
TSum_list
= (8+7n)
Similarly, TSum_matrix = an2 +
bn + c
Now before moving any further you need to study
about the asymptotic functions. If you already knew about these asymptotic
notations then you can proceed.
The time complexity
is commonly expressed using big O notation.So, here TSum =
O(1)
TSum_list
= O(n)
TSum_matrix
= O(n2)
You know what is big
O notation, if not then read this first.
For getting the definable value for time complexity you need to know about Time Complexity Analysis.
To have it all done regarding time complexity, you also need to know about the time complexity analysis too.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via instagram "@coding.winds".
We also have an article on space complexity, give it a read by clicking here.
No comments:
Post a Comment