I am trying to use hash tables in Haskell with the hashtables package, and finding that I cannot get anywhere near Python's performance. How can I achieve similar performance? Is it possible given current Haskell libraries and compilers? If not, what's the underlying issue?

Here is my Python code:

y = {} for x in xrange(10000000): y[x] = x print y[100]

Here's my corresponding Haskell code:

import qualified Data.HashTable.IO as H import Control.Monad main = do y <- H.new :: IO (H.CuckooHashTable Int Int) forM_ [1..10000000] $ \x -> H.insert y x x H.lookup y 100 >>= print

Here is another version using Data.Map , which is slower than both for me:

import qualified Data.Map as Map import Data.List import Control.Monad main = do let m = foldl' (\m x -> Map.insert x x m) Map.empty [1..10000000] print $ Map.lookup 100 m

Interestingly enough, Data.HashMap performs very badly:

import qualified Data.HashMap.Strict as Map import Data.List main = do let m = foldl' (\m x -> Map.insert x x m) Map.empty [1..10000000] print $ Map.lookup 100 m

My suspicion is that Data.HashMap performs badly because unlike Data.Map , it is not spine-strict (I think), so foldl' is just a foldl , with the associated thunk buildup problems.