What is Big O notation? Do you use it?
This question already has an answer here:
One important thing most people forget when talking about Big-O, thus I feel the need to mention that:
You cannot use Big-O to compare the speed of two algorithms. Big-O only says how much slower an algorithm will get (approximately) if you double the number of items processed, or how much faster it will get if you cut the number in half.
However, if you have two entirely different algorithms and one ( A
) is O(n^2)
and the other one ( B
) is O(log n)
, it is not said that A
is slower than B
. Actually, with 100 items, A
might be ten times faster than B
. It only says that with 200 items, A
will grow slower by the factor n^2
and B
will grow slower by the factor log n
. So, if you benchmark both and you know how much time A
takes to process 100 items, and how much time B
needs for the same 100 items, and A
is faster than B
, you can calculate at what amount of items B
will overtake A
in speed (as the speed of B
decreases much slower than the one of A
, it will overtake A
sooner or later—this is for sure).
Big O notation denotes the limiting factor of an algorithm. Its a simplified expression of how run time of an algorithm scales with relation to the input.
For example (in Java):
/** Takes an array of strings and concatenates them
* This is a silly way of doing things but it gets the
* point across hopefully
* @param strings the array of strings to concatenate
* @returns a string that is a result of the concatenation of all the strings
* in the array
*/
public static String badConcat(String[] Strings){
String totalString = "";
for(String s : strings) {
for(int i = 0; i < s.length(); i++){
totalString += s.charAt(i);
}
}
return totalString;
}
Now think about what this is actually doing. It is going through every character of input and adding them together. This seems straightforward. The problem is that String is immutable . So every time you add a letter onto the string you have to create a new String. To do this you have to copy the values from the old string into the new string and add the new character.
This means you will be copying the first letter n times where n is the number of characters in the input. You will be copying the character n-1
times, so in total there will be (n-1)(n/2)
copies.
This is (n^2-n)/2
and for Big O notation we use only the highest magnitude factor (usually) and drop any constants that are multiplied by it and we end up with O(n^2)
.
Using something like a StringBuilder
will be along the lines of O(nLog(n)). If you calculate the number of characters at the beginning and set the capacity of the StringBuilder
you can get it to be O(n)
.
So if we had 1000 characters of input, the first example would perform roughly a million operations, StringBuilder
would perform 10,000, and the StringBuilder
with setCapacity
would perform 1000 operations to do the same thing. This is rough estimate, but O(n)
notation is about orders of magnitudes, not exact runtime.
It's not something I use per say on a regular basis. It is, however, constantly in the back of my mind when trying to figure out the best algorithm for doing something.
A very similar question has already been asked at Big-O for Eight Year Olds?. Hopefully the answers there will answer your question although the question asker there did have a bit of mathematical knowledge about it all which you may not have so clarify if you need a fuller explanation.
链接地址: http://www.djcxy.com/p/6742.html上一篇: O(1),O(n),O(n * n)记忆的含义是什么?
下一篇: 大O符号是什么? 你用它吗?