public
Last active

Map-like structure benchmarks

  • Download Gist
TestHashTableArtificial.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
module TestHashTableArtificial where
 
import Control.Monad (forM_)
import qualified Data.HashTable as HashTable
 
-- ByteString IO
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.UTF8 as UTF8
 
-- ByteString hashing
import qualified Data.ByteString.Internal as ByteString
import qualified Data.ByteString.Unsafe as ByteString
import Data.Hashable (hashPtr)
 
hashByteString bs = ByteString.inlinePerformIO $
ByteString.unsafeUseAsCStringLen bs $ \(p, len) ->
hashPtr p (fromIntegral len)
--
 
sizeHint = 1500000
 
mainByteString =
do ht <- HashTable.newHint (==) (fromIntegral . hashByteString) sizeHint
forM_ (take sizeHint (map (UTF8.fromString . show) [1..]))
(\v -> HashTable.insert ht v 1)
maybeVal <- HashTable.lookup ht (UTF8.fromString $ show sizeHint)
case maybeVal of
Nothing -> return ()
Just v -> print v
 
mainString =
do ht <- HashTable.newHint (==) HashTable.hashString sizeHint
forM_ (take sizeHint (map show [1..])) (\v -> HashTable.insert ht v 1)
maybeVal <- HashTable.lookup ht (show sizeHint)
case maybeVal of
Nothing -> return ()
Just v -> print v
 
mainInt =
do ht <- HashTable.newHint (==) HashTable.hashInt sizeHint
forM_ (take sizeHint ([1..])) (\v -> HashTable.insert ht v 1)
maybeVal <- HashTable.lookup ht sizeHint
case maybeVal of
Nothing -> return ()
Just v -> print v
 
{-main = mainInt-}
{-main = mainString-}
main = mainByteString
TestHashTableArtificial.txt
1 2 3 4 5 6 7 8 9 10 11
20:55:24 erszcz @ x : ~/work/pjn/spellchecker
$ ghc --make -main-is TestHashTableArtificial.main TestHashTableArtificial.hs
20:55:31 erszcz @ x : ~/work/pjn/spellchecker
$ time ./TestHashTableArtificial
1
 
real 0m11.524s
user 0m11.097s
sys 0m0.380s
20:55:50 erszcz @ x : ~/work/pjn/spellchecker
$
TestReadIntoHashTable.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
module TestReadIntoHashTable where
 
import Control.Monad (forM_)
import qualified Data.HashTable as HashTable
 
-- ByteString IO
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.UTF8 as UTF8
 
-- ByteString hashing
import qualified Data.ByteString.Internal as ByteString
import qualified Data.ByteString.Unsafe as ByteString
import Data.Hashable (hashPtr)
 
hashByteString bs = ByteString.inlinePerformIO $
ByteString.unsafeUseAsCStringLen bs $ \(p, len) ->
hashPtr p (fromIntegral len)
 
sizeHint = 1400000
 
buildDict words = do ht <- HashTable.newHint (==)
(fromIntegral . hashByteString) sizeHint
forM_ words (mkUpdate ht)
return ht
 
mkUpdate ht = \w -> do maybeCount <- HashTable.lookup ht w
case maybeCount of
Nothing ->
do HashTable.insert ht w 1
return False
Just count ->
do HashTable.update ht w (count+1)
 
{-getWords = do contents <- ByteString.readFile "data/formy-short.utf8"-}
getWords = do contents <- ByteString.readFile "data/formy.utf8"
{-getWords = do contents <- ByteString.readFile "data/formy-mult.utf8"-}
buildDict (UTF8.lines contents)
 
main = do words <- getWords
maybeVal <- HashTable.lookup words $ UTF8.fromString "┼╝y┼║niejszymi"
case maybeVal of
Nothing -> return ()
Just v -> print v
TestReadIntoHashTable.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
20:39:44 erszcz @ x : ~/work/pjn/spellchecker
$ ghc --make -main-is TestReadIntoHashTable.main TestReadIntoHashTable.hs
[1 of 1] Compiling TestReadIntoHashTable ( TestReadIntoHashTable.hs, TestReadable.o )
Linking TestReadIntoHashTable ...
20:40:05 erszcz @ x : ~/work/pjn/spellchecker
$ time ./TestReadIntoHashTable
^C
real 11m2.574s
user 10m58.841s
sys 0m0.324s
 
20:51:08 erszcz @ x : ~/work/pjn/spellchecker
$

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.