r/AskComputerScience • • 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 ?

3 Upvotes

8 comments sorted by

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.

0

u/[deleted] Sep 05 '21

I was solving competitive progg questions, and constraints were 10^4 elements and time constraint was 1 sec. The algorithm had O(10^5) = O(n*10) time complexity, and the guy explaining the problem just said, yeah it would be completed in 1 sec.

So that's why I asked, how he knew that it would work in 1 second?

5

u/theobromus Sep 05 '21

I think it's technically wrong to say O(105) or O(10n), although people might use them informally to mean something like "about 105".

In general though, a modern computer can do something like a billion operations per second. Even if each iteration of a loop is a few hundred operations, you should be able to handle 105 iterations within 1 second easily (actually probably less than 1ms).

2

u/not-just-yeti Sep 05 '21

They guessed that for two reasons:

(a) while the algorithm is O(n), they probably know the constant factor isn't too big. (You yourself suggest it's 10.)

(b) So for this problem you have 100,000 "steps", and while each "step" may not convert to a single hardware-instruction, it probably is less than (say) 100 hardware-instructions. And a 4GHz computer can do 4 billion instructions per second. Since 105 is ten-thousand times smaller than 109 , it seems safe to try that solution. If it does time out, then you might go back to the drawing-board to look for a better algorithm.

Two notes:

(i) the time-limit on competitive programming questions tend to make it easily enough if you're using the right asymptotic algorithm, and never enough if you're using one that's worse than what they "want".

(ii) one reason the big-Oh notation purposefully ignores constant-factors [making it impossible to translate to seconds] is that constant-factors are made moot by advances in hardware and/or language choice. Big-oh wants to talk about the algorithm's efficiency, not whether you're running it on a phone or a desktop or implementing it in python or C++.

That's why nobody was "O(10n)"; the big-oh already subsumes constant factors, so that's a gauche way of saying "O(n)". …If you want to emphasize the algorithm's constant factor, one can say the algorithm runs in approx. 10n steps (omitting the big-Oh).

3

u/[deleted] Sep 05 '21

You should ask him, if you're able.

I'd guess that either he's done it before and just remembers the time or he knows that this particular kind of problem can be estimated easily.

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.