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

后端 未结 23 1989
广开言路
广开言路 2020-12-22 18:17

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?

23条回答
  •  自闭症患者
    2020-12-22 18:57

    Assume that in your entire application, one algorithm accounts for 90% of the time the user waits for the most common operation.

    Suppose in real time the O(1) operation takes a second on your architecture, and the O(logN) operation is basically .5 seconds * log(N). Well, at this point I'd really like to draw you a graph with an arrow at the intersection of the curve and the line, saying, "It matters right here." You want to use the log(N) op for small datasets and the O(1) op for large datasets, in such a scenario.

    Big-O notation and performance optimization is an academic exercise rather than delivering real value to the user for operations that are already cheap, but if it's an expensive operation on a critical path, then you bet it matters!

提交回复
热议问题