What does “O(1) access time” mean?

后端 未结 16 2112
栀梦
栀梦 2020-11-28 18:21

I have seen this term \"O(1) access time\" used to mean \"quickly\" but I don\'t understand what it means. The other term that I see with it in the same context is \"O(n) ac

相关标签:
16条回答
  • 2020-11-28 18:32

    "Big O notation" is a way to express the speed of algorithms. n is the amount of data the algorithm is working with. O(1) means that, no matter how much data, it will execute in constant time. O(n) means that it is proportional to the amount of data.

    0 讨论(0)
  • 2020-11-28 18:33

    In essence, It means that it takes the same amount of time to look up a value in your collection whether you have a small number of items in your collection or very very many (within the constraints of your hardware)

    O(n) would mean that the time it takes to look up an item is proportional to the number of items in the collection.

    Typical examples of these are arrays, which can be accessed directly, regardless of their size, and linked lists, which must be traversed in order from the beginning to access a given item.

    The other operation usually discussed is insert. A collection can be O(1) for access but O(n) for insert. In fact an array has exactly this behavior, because to insert an item in the middle, You would have to move each item to the right by copying it into the following slot.

    0 讨论(0)
  • 2020-11-28 18:33

    It's called the Big O notation, and describes the search time for various algorithms.

    O(1) means that the worst case run time is constant. For most situation it means that you dont acctually need to search the collection, you cand find what you are searching for right away.

    0 讨论(0)
  • 2020-11-28 18:35

    O(1) does not necessarily mean "quickly". It means that the time it takes is constant, and not based on the size of the input to the function. Constant could be fast or slow. O(n) means that the time the function takes will change in direct proportion to the size of the input to the function, denoted by n. Again, it could be fast or slow, but it will get slower as the size of n increases.

    0 讨论(0)
  • 2020-11-28 18:35

    Here is a simple analogy; Imagine you are downloading movies online, with O(1), if it takes 5 minutes to download one movie, it will still take the same time to download 20 movies. So it doesn't matter how many movies you are downloading, they will take the same time(5 minutes) whether it's one or 20 movies. A normal example of this analogy is when you go to a movie library, whether you are taking one movie or 5, you will simply just pick them at once. Hence spending the same time.

    However, with O(n), if it takes 5 minutes to download one movie, it will take about 50 minutes to download 10 movies. So time is not constant or is somehow proportional to the number of movies you are downloading.

    0 讨论(0)
  • 2020-11-28 18:37

    Basically, O(1) means its computation time is constant, while O(n) means it will depend lineally on the size of input - i.e. looping through an array has O(n) - just looping -, because it depends on the number of items, while calculating the maximum between to ordinary numbers has O(1).

    Wikipedia might help as well: http://en.wikipedia.org/wiki/Computational_complexity_theory

    0 讨论(0)
提交回复
热议问题