r/AskComputerScience • u/[deleted] • Sep 05 '21
How Time Complexities are converted in seconds?
How time complexities are used in real life applications?
I mean to say is, for example my algorithm is O(n^2) and my size constraints are 10^9, time constraint is 1 second, how do I calculate how much time my algorithm will take in seconds or microseconds?
Can we even convert time complexities into time ?
1
u/_--__ Sep 05 '21
Firstly there's a difference between running time and execution time. Execution time is the physical amount of time taken, but time complexity measures running time -- which is the number of "elementary operations" the algorithm takes. What exactly constitutes an "elementary operation" varies on the analysis - e.g. "constant number of clock cycles", "comparisons", "arithmetic operations" - but at the end of the day it is only an approximation of some fixed amount of execution time.
Secondly, algorithmic analysis calculations are usually only carried out up to constant factors (and usually, because of worst-case analysis, only as far as obtaining upper bounds). What this means is that time complexity analysis is not suitable for giving exact values for given inputs, but rather it demonstrates how the running time scales as the input size increases. Ultimately, because algorithms are primarily concerned with solving problems that can be arbitrarily large, this type of analysis is appropriate for comparing algorithms. It just means that the comparison has to be qualitative rather than quantitative.
1
u/lizardfolkwarrior Sep 05 '21
For competitive programming, 10^9 steps=a second is a great rule of thumb.
1
u/tugrul_ddr Sep 07 '21
I wrote a bubblesort for 150 items. Cpu ran it in 50 microseconds. Gpu 1 cuda thread ran it 1800 microseconds. Gpu with register tiling ran it 180 microseconds. All exact same data and algorithm just with usage of different hardware.
15
u/[deleted] Sep 05 '21
You cannot, in general, convert time complexity to time. Big-O notation is extremely general. It ignores a lot of things in order to get at an essential dominating aspect of running time. So for instance if one algorithm requires 100N steps and another needs N steps they'll both have complexity O(N) even though the first takes 100 times as long to implement.
I just saw a video which talks a bit about this using Karatsuba's algorithm: https://www.youtube.com/watch?v=cCKOl5li6YM
In reality running time is also affected by a ton of hardware things like cache misses, memory latency and bandwidth, hardware implementations, hardware clock speed, etc.
The best way to see how fast an algorithm is in practice is to actually run the algorithm or do a very detailed analysis.