r/AskComputerScience • u/[deleted] • Jul 13 '22
how useful is big O, really?
probably a moronic question.
but say you have 2 algorithms and you determine both are O(log n). does this mean both algorithms are just as good as one another?
to be clear, in no way am I saying you shouldn't analyze your program/algorithm. just wondering about the utility of big O itself.
EDIT: I should have said time and space complexity is O(log n).
5
u/DiaperBatteries Jul 13 '22
From a practical standpoint, big-O is an incredibly useful metric for determining which algorithms or data structures to choose. However, for performance critical things you can’t beat a good benchmark.
For example, when you want a structure to perform lookups in and you have to decide between between a tree map (O(logn)), a hashmap (O(1)) and just a vector of pairs (O(n)), the hashmap will be the obvious choice if you are going to be storing 100,000 elements and doing frequent lookups. But if you’re storing 10 elements at most, the O(n) lookup of the vector of pairs might actually be the most performant.
To figure that out, you’d have to run some benchmarks. Understanding time complexity allows you to determine when there’s clearly a best option and when you’ll have to do more work to figure out which option is the best.
2
Jul 14 '22
From a practical standpoint, big-O is an incredibly useful metric for determining which algorithms or data structures to choose. However, for performance critical things you can’t beat a good benchmark.
Nice thanks.
2
u/anamorphism Jul 13 '22
big o is merely notation describing the growth rate of a thing (generally the number of operations, but you could calculate big o for something like memory use) as your input grows.
it doesn't really give you any information about actual numbers or time or anything like that.
you could have an algorithm that iterates over the entire input collection and simply calls a pause of 1 ms for each item, and you can have another that pauses for a trillion milliseconds in each loop iteration. both of these are going to be O(n).
but the concept has come up pretty frequently during my 15ish year career. it's a good way of communicating general efficiency of something with minimal words.
kind of like stating "there is one verb in this sentence" and not "there are four words that are used to describe an action taking place in this sentence."
1
Jul 13 '22
you could have an algorithm that iterates over the entire input collection and simply calls a pause of 1 ms for each item, and you can have another that pauses for a trillion milliseconds in each loop iteration. both of these are going to be O(n).
I like this.
it's a good way of communicating general efficiency of something with minimal words.
Sounds good.
3
u/arlitsa Jul 13 '22
just because it's big O computationally, does not mean it's big O in other dimensions like in memory space. If both are log n computationally, but one uses O(1) memory while the other uses O(n2) memory, I'd choose the first. Different domains (e.g. embedded computing vs HPC) may seek different qualities.
In practice, very rarely do I think of it, but it has come in handy a couple times before.
1
Jul 13 '22
Good point about memory usage. I was vague. I should have said space complexity is the same as well. Will edit the question.
2
u/Skerilyo Jul 13 '22
Not really ... big O defines the way your number of instructions processed increase with an increase of n, but there is a multiplicative variable k that is put aside in that notation, because the notation cares about when n tends toward infinity
So, an algorithm with O(log n) process time will increase slower that a O(n) algorithms, so eventually with a n great enough Algo 1 will have to process less instructions that Algo 2, but if k is 8756 for Algo 1 and 2 for Algo 2, then for the number of processed instructions will be:
8756 for n = 10 for Algo1 10 for n = 10 for Algo 2
A good example are algorithms to calculate multiplication of matrix Basic Algo has O(n³) and more useful one has O(n².⁸³) but that k variable is high so overhaul the second Algo is faster only for matrix greater that 50x50 if I recall correctly
Anyway, if 2 algorithms are in O(log n) but one of them has a k variable equal to 555555 and the other one a k variable equal to 2, second algorithm is technically better than first one
1
Jul 13 '22
what do you mean by k variable ?
1
u/Skerilyo Jul 13 '22
When you do your calculations of the big O you end up with something like:
"Time complexity of this algorithm is 78*log(n)" So it's a O(log(n)) algorithm, because when n is infinite, this 78 multiplicative constant which we name k is insignificant
So you can have an Algo with a time complexity of O(log(n)) with your calcul results being like ... 3*log(n)
And an Algo with a time complexity of O(log(n)) with a calcul results being like 765*log(n)
Second one is worse than first one but they are both O(log(n))
Big O notation serve to represent magnitude of speed at which the time complexity increase with n getting bigger Those two Algo time complexity increase at the same speed up to one multiplicative constant Whereas an algo in O(n²)'s time complexity increase quicker than an algo in O(log(n))
(Sry English isn't my main language ...)
1
2
u/AbouBenAdhem Jul 13 '22
If your input is always of fixed size, then no, big O doesn’t matter.
But if the input is of variable size, your algorithm will spend a disproportionate amount of time on the largest-size inputs—and big O tells you how disproportionate that will be.
2
Jul 14 '22
If your input is always of fixed size, then no, big O doesn’t matter.
maybe it can?
contrived example. say your input is always an array of 4 billion sorted ints. the goal is see if a given int exists in the array. wouldn't binary search still be better than the naive approach (ie loop through all items) ?
2
u/theobromus Jul 13 '22
Not a moronic question at all.
Whether big O matters depends on the context. For the example you gave about two algorithms that have O(log n), the answer is clearly no - there could be huge constant factor differences that matter a ton in practice. For example, a b-tree might be a lot faster than a binary tree, even if both are O(log n).
There can even be cases where the asymptotic behavior takes so long to kick in that there's no practical reason to use an algorithm even if it has a better big-O. Some examples I can think of like that include things like the AKS primality test (which is asymptotically faster (polynomial in the number of bits), but in practice slower than simpler approaches even for extremely large numbers. Another example is probably Fibonacci heaps.
Now, there are many cases where the asymptotic behavior catches up to you quite quickly. For example, quicksort is going to beat any of the O(n2 ) sorts even for quite small n.
The main advantage of big-O analysis is that it doesn't depend on the specifics of a certain implementation or hardware, while the actual runtimes certainly will.
2
u/NukeyFox Jul 13 '22
Others have said good things about big-O already. I want to add that big-O analysis is really a general method to give an upper bound of some quantity depending on how another quantity grows.
Outside the context of programming, I've seen examples of big-O being used to analyze:
...how the number of edges grow in a network changes as the number of nodes in a network grows,
...the energy consumption of a chip scales with the number of components,
...the smallest number of lines of certain proofs given a logical system
1
u/UntangledQubit Jun 07 '23
Big O technically ignores the constant factors. However in practice, the constant factors are usually bounded. We don't really get pairs of algorithms where the constant factors differ by e.g. 100,000. So when n varies enough, you can be confident that the better-big-O algorithm will be better, just because of the typical properties of human-written algorithms.
There's an interesting range of parameters where this heuristic doesn't work. In practice n is also bounded, since we only have some finite amount of data we process on modern computers. When algorithms differ by much less than a full power, you can get into situations where even with large realistic n, the 'slower' big O is better. This is the case with e.g. matrix multiplication, where we can improve from n2.8 to n2.4, but the constants are so large that we just stick to the 2.8.
0
u/RIP_lurking Jul 13 '22
Assuming that both of these algorithms serve the same purpose, then yes. Asymptotic notation is like <=, but for functions (in fact, it is a partial order on functions).
41
u/mcherm Jul 13 '22
Absolutely not. One algorithm might run 30x faster than the other. Or one might run faster on the size of data that your system sees typically. Or one might take far less memory. Or might be more cache-friendly. Most commonly of all, one algorithm might be far simpler than another (and therefore less prone to bugs).
Despite the above, it is QUITE useful. I can take 20 minutes to describe algorithm A, then another 20 minutes to describe algorithm B, then another 20 minutes to describe algorithm C. Then for my second hour, I can describe some of the various strengths and weaknesses of the algorithms. After a couple hours of discussion you might be able to understand those three options and decide which is appropriate for your use case.
OR, I could spend 30 seconds telling you that one algorithm is O(n2) while the other is O(n log n). You immediately know that if you're going to be working on data values where n is in the thousands or more, that the second algorithm is going to be enormously better.
Basically, big O captures only one particular dimension of the differences between algorithms -- the degree to which that algorithm "blows up" when working on large problems. But that dimension is perhaps the single MOST important dimension to consider, and big O captures it quite cleanly.
As a working programmer focused on delivering working software, rather than on theoretical computer science, I would not expect the average mid-level (or even senior) developer on my team to be able to perform the mathematical analysis of an algorithm to determine it's average-case big-O performance. But I absolutely WOULD expect the average mid-level developer on my team to know the big-O performance of different data structures for different purposes and to make specific choices based on that information like "I'll keep this in a list, but ALSO in a hashtable because of the access patterns" or "I'll use a bloom filter here to get 10X performance improvement".