Editing Hidden constant factor

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 5: Line 5:
 
The discrepancy in hidden constant factor between two algorithms with the same asymptotic running time (big O) is a consequence of three main factors:
 
The discrepancy in hidden constant factor between two algorithms with the same asymptotic running time (big O) is a consequence of three main factors:
 
* Some algorithms, by nature, simply require more operations than others. [[Bubble sort]], for example, tends to use more operations than [[insertion sort]]. Bubble sort can only reorder elements by swapping two adjacent elements at a time, and swapping two elements requires three copy operations (as an intermediate variable has to be used), and swapping two elements eliminates an inversion from the sequence. On the other hand, insertion sort moves elements longer distances at once. When an element is moved <math>m</math> positions, it eliminates <math>m</math> inversions, and requires <math>m+2</math> copy operations; and thus will insertion sort will generally average a bit more than one copy operation per inversion.
 
* Some algorithms, by nature, simply require more operations than others. [[Bubble sort]], for example, tends to use more operations than [[insertion sort]]. Bubble sort can only reorder elements by swapping two adjacent elements at a time, and swapping two elements requires three copy operations (as an intermediate variable has to be used), and swapping two elements eliminates an inversion from the sequence. On the other hand, insertion sort moves elements longer distances at once. When an element is moved <math>m</math> positions, it eliminates <math>m</math> inversions, and requires <math>m+2</math> copy operations; and thus will insertion sort will generally average a bit more than one copy operation per inversion.
* Some operations are slower than others. For example, multiplication and division of floating point numbers tends to be slower than addition and subtraction. Thus, for example, if a primitive in [[computational geometry]] can be implemented using either six additions and two multiplications or four additions and three multiplications, both implementations take constant time, but the former is probably faster.
+
* Some operations are slower than others. For example, multiplication and division of floating point numbers tends to be slower than addition and subtraction. Thus, for example, if a primitive in [[computational geometry]] can be implemented using either six additions and two multiplications or four additions and three multiplications, both implementations take constant time, but the latter is probably faster.
 
* Some algorithms exhibit better locality of reference than others. For example, two nested for loops that iterate over a two-dimensional [[array]] should always be written so that they access the elements of the array in sequence in RAM, rather than in the other order. For example, in C this means that they should access elements in the other <code>A[0][0], A[0][1], A[0][2], ..., A[1][0], ...</code> rather than in the order <code>A[0][0], A[1][0], A[2][0], ..., A[0][1], ...</code>. The former hits the cache on almost every access; the latter always misses it.
 
* Some algorithms exhibit better locality of reference than others. For example, two nested for loops that iterate over a two-dimensional [[array]] should always be written so that they access the elements of the array in sequence in RAM, rather than in the other order. For example, in C this means that they should access elements in the other <code>A[0][0], A[0][1], A[0][2], ..., A[1][0], ...</code> rather than in the order <code>A[0][0], A[1][0], A[2][0], ..., A[0][1], ...</code>. The former hits the cache on almost every access; the latter always misses it.
  

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)