Why does RAM access any memory address in O(1) time?

回眸只為那壹抹淺笑 提交于 2019-12-08 12:20:27

问题


When we work with arrays, for example, assign a[10000] = 3, what it does is it first add 10000 * sizeof(int) to memory address of a, and then access that memory address. The question is, why does accessing such memory address takes only O(1) time? Basically, what's the magic behind random access memory such that it takes the same time accessing address #1 vs. address #1,000,000?


回答1:


Random access memory is by definition memory that can provide the contents at any address in a constant amount of time. That's the 'random access' bit, and what distinguishes it from other types of memory.

There's no magic involved; there are just n locations, and whomever is accessing memory provides the details to select one of them.

In most implementations every word of memory is an identical piece of electronics. You're just selecting which is connected to the bus.




回答2:


for example, assign a[10000] = 3, what it does is it first add 10000 * sizeof(int) to memory address of a, and then access that memory address.

That's exactly right. Many CPUs have a single addressing mode to access data at an offset from the base, where the base is a fixed number and the offset is in a register, or vice versa.

what's the magic behind random access memory such that it takes the same time accessing address #1 vs. address #1,000,000?

The same "magic" that lets you add two numbers in a fixed amount of time, regardless of the values of the numbers being added. In other words, it does not matter if you add 1 or 1000000 to the base of, say, 2000000000: it takes the same number of cycles.



来源:https://stackoverflow.com/questions/51813009/why-does-ram-access-any-memory-address-in-o1-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!