[Haskell-cafe] implementing python-style dictionary in Haskell

Claus Reinke claus.reinke at talk21.com
Tue Nov 18 06:33:25 EST 2008


> main :: IO ()
> main = do { content <- readFile "in.txt"
>      ; let -- change this following type annotation
>            -- to change different type of the dictionary
>            -- dict :: DM.Map S.ByteString Int
>            -- dict :: IM.IntMap Int
>            dict :: Trie Int
>            dict = fromList (map parse_a_line  (lines content))
> ..
>    where  parse_a_line :: String -> (Key,Int)
>           parse_a_line line = case words line of
>                  [key,val] -> (key,read val)
>                  _ -> error " parse error.  "

Maps tend to be strict in their keys, but not in their values.
You might be storing a lot of thunks with unparsed Strings 
instead of plain Int values.

Something like this might make a difference wrt memory usage:

           [key,val] -> ((,) key) $! (read val)

Hth,
Claus

> Here is a comparison of memory usage
> 
> Map     : 345 MB
> IntMap : 146 MB
> Trie     : 282 MB
> Python : 94 MB



More information about the Haskell-Cafe mailing list