DEV Community

Cover image for Hash table efficiency example
MoeHdaib
MoeHdaib

Posted on

Hash table efficiency example

Hash tables is a data structure that can map keys to values which means that it gives every item in a list has key that is used to extract it later.
Its known that hash tables has better performance than normal list but how its differ?

I wrote a code to compare the time it gets for a list to extract an item in reletively big data structure as well as in a hash table.

First, we fill the list and the hash table with 5000000 values.

Alt Text

Then we pick a reandom value between 4 million and 5 million to get the worst case. After that, we calculate the time it takes for the list and the hash table to extract the value.

Alt Text

After we execute the code the results are:

Alt Text

As you can see the difference between the time. The hash table in all the tests took 0 ms but the list took between 34 to 42 ms to extract the desired values.

If you want test the code yourself check my GitHub

Top comments (0)