Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@erszcz
Created April 18, 2012 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erszcz/2411699 to your computer and use it in GitHub Desktop.
Save erszcz/2411699 to your computer and use it in GitHub Desktop.
Map-like structure benchmarks
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
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
$
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
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
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment