简体   繁体   中英

Why is Map slower than Dictionary on reads, in F# (from the benchmark I have attached)

I stumbled across this page: http://jackfoxy.com/simple-lookups-in-fsharp/

It benchmarks various collections for insertion and retrieval.

If we look at this table (the second on the web page):

String Key by Number of Elements in Lookup Structure, 10,000 random lookups
        Map     IntMap  Dict    HashTbl     HshMltMap
10^2    1.3     n/a     0.4     0.3     1.5
10^3    1.7     n/a     0.4     0.4     1.5
10^4    3.0     n/a     0.7     0.7     1.8
10^5    5.3     n/a     1.5     1.2     2.4
10^6    8.4     n/a     1.6     1.5     6.3

We can see that the lookup with a Map is 5x slower than the Dictionary when it becomes large.

Since Map is read-only and has therefore all the luxury to organize the data in the most optimal way as it doesn't deal with insertions, resizing, etc.. why is it so much slower?

C# Dictionary is implemented using hash tables thus speed of lookup is closed to O(1) .

F# Map, due to the immutability, must use binary trees thus speed of lookup is O(logN) .

F# Map, and other immutable data structures, actually deal with insertions. You still need to insert an element to a map, but get a new map, and the old map is still intact. To make this effective, the new one and the old one must share elements (no cloning/duplicating involved).

And yes, the best way to do that is using trees (F# Map, Set) or linked lists (F# List).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM