Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are arrays and hash maps constant time in their access?

Specifically: given a hash (or an array index), how does the machine get to the data in constant time?

It seems to me that even passing by all the other memory locations (or whatever) would take an amount of time equal to the number of locations passed (so linear time). A coworker has tried valiantly to explain this to me but had to give up when we got down to circuits.

Example:

my_array = new array(:size => 20)
my_array[20] = "foo"
my_array[20] # "foo"

Access of "foo" in position 20 is constant because we know which bucket "foo" is in. How did we magically get to that bucket without passing all the others on the way? To get to house #20 on a block you would still have to pass by the other 19...

like image 349
keithcelt Avatar asked Jan 13 '11 23:01

keithcelt


People also ask

Are hash maps constant time?

Thus, everyone knows that hash table queries run in amortized constant time. That is, as the number of keys increases, the average time necessary to recover a key-value pair does not increase.

Why is hash table constant time?

It is technically true because the hash function is not required to use all the information in the key and so could be constant time, and because a large enough table can bring collisions down to near constant time.

How does HashMap work in javascript?

Hashmaps are organized as linked lists, so the order of its elements is maintained, which allows the hashmap to be iterable. Also, unlike key/value pairs stored in an Object, the number of items in a hashmap can be determined by the size property.


4 Answers

How did we magically get to that bucket without passing all the others on the way?

"We" don't "go" to the bucket at all. The way RAM physically works is more like broadcasting the bucket's number on a channel on which all buckets listen, and the one whose number was called will send you its contents.

Calculations happen in the CPU. In theory, the CPU is the same "distance" from all memory locations (in practice it's not, because of caching, which can have a huge impact on performance).

If you want the gritty details, read "What every programmer should know about memory".

like image 190
Michael Borgwardt Avatar answered Oct 22 '22 04:10

Michael Borgwardt


Then to understand you have to look at how memory is organized and accessed. You may have to look at the way an address decoder works. The thing is, you do NOT have to pass by all the other addresses to get to the one you want in the memory. You can actually jump to the one you want. Otherwise our computers would be really really slow.

like image 31
Vincent Ramdhanie Avatar answered Oct 22 '22 05:10

Vincent Ramdhanie


Unlike a turing machine, which would have to access memory sequentially, computers use random access memory, or RAM, which means if they know where the array starts and they know they want to access the 20th element of the array, they know what part of memory to look at.

It is less like driving down a street and more like picking the correct mail slot for your apartment in a shared mailbox.

like image 21
James Avatar answered Oct 22 '22 04:10

James


2 things are important:

  1. my_array has information about where in memory computer must jump to get this array.
  2. index * sizeof type gets offset from beginning of array.

1 + 2 = O(1) where data can be found

like image 23
lukastymo Avatar answered Oct 22 '22 05:10

lukastymo