packages feed

PerfectHash-0.1: benchmark_trie.hs

{-# LANGUAGE ScopedTypeVariables #-}

import Random
import HackedMicrobench -- small bugfix - use Integer rather than Int. original author unavailable for reports
import Control.Exception
import Data.List as List
import Data.Trie as Trie
import Maybe
import qualified Data.ByteString.Char8 as S


trie_lookup :: Trie.Trie Integer -> [S.ByteString] -> Integer -> Integer
trie_lookup m words size = sum $ mapMaybe (tl m) targets
 where targets :: [S.ByteString] = genericTake size $ cycle words

tl string trie = {-# SCC "trie" #-} Trie.lookup trie string

main = do
 str <- S.readFile "/usr/share/dict/words"
 words <- evaluate $ S.lines str
 source :: [(S.ByteString, Integer)] <- evaluate $ zip words [1..]
 trie <- evaluate $ Trie.fromList source
 {-# SCC "mb_lookup" #-} microbench "trie lookup" (trie_lookup   trie words)