O(log N) == O(1) - Why not?

phoku picture phoku · Sep 29, 2009 · Viewed 16.2k times · Source

Whenever I consider algorithms/data structures I tend to replace the log(N) parts by constants. Oh, I know log(N) diverges - but does it matter in real world applications?

log(infinity) < 100 for all practical purposes.

I am really curious for real world examples where this doesn't hold.

To clarify:

  • I understand O(f(N))
  • I am curious about real world examples where the asymptotic behaviour matters more than the constants of the actual performance.
  • If log(N) can be replaced by a constant it still can be replaced by a constant in O( N log N).

This question is for the sake of (a) entertainment and (b) to gather arguments to use if I run (again) into a controversy about the performance of a design.

Answer

Brian R. Bondy picture Brian R. Bondy · Sep 29, 2009

Big O notation tells you about how your algorithm changes with growing input. O(1) tells you it doesn't matter how much your input grows, the algorithm will always be just as fast. O(logn) says that the algorithm will be fast, but as your input grows it will take a little longer.

O(1) and O(logn) makes a big diference when you start to combine algorithms.

Take doing joins with indexes for example. If you could do a join in O(1) instead of O(logn) you would have huge performance gains. For example with O(1) you can join any amount of times and you still have O(1). But with O(logn) you need to multiply the operation count by logn each time.

For large inputs, if you had an algorithm that was O(n^2) already, you would much rather do an operation that was O(1) inside, and not O(logn) inside.

Also remember that Big-O of anything can have a constant overhead. Let's say that constant overhead is 1 million. With O(1) that constant overhead does not amplify the number of operations as much as O(logn) does.

Another point is that everyone thinks of O(logn) representing n elements of a tree data structure for example. But it could be anything including bytes in a file.