Skip to content

Instantly share code, notes, and snippets.

@bitonic
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bitonic/781ff80e6eb65eebf14a to your computer and use it in GitHub Desktop.
Save bitonic/781ff80e6eb65eebf14a to your computer and use it in GitHub Desktop.
import Control.Exception (evaluate)
import Control.Monad (forM_)
import Control.Monad (replicateM, forM)
import qualified Data.ByteString.Lazy as BS
import qualified Data.Digest.CRC32
import qualified Data.Digest.Pure.CRC32
import System.Environment (getArgs)
import qualified System.Random.MWC as MWC
import Data.Binary (encodeFile, decodeFile)
import Data.Word (Word32)
randomByteString
:: MWC.GenIO
-> Int
-- ^ Desired length
-> IO BS.ByteString
randomByteString gen n = do
bytes <- replicateM n $ MWC.uniform gen
return $ BS.pack bytes
bench
:: (BS.ByteString -> Word32)
-- ^ CRC32 function
-> [BS.ByteString] -> IO ()
bench digest bss = do
forM_ bss $ \bs -> do
forM_ [1..1000] $ \n -> do
evaluate $ digest $ BS.cons (fromIntegral n) bs
main :: IO ()
main = do
args <- getArgs
case args of
["generate"] -> do
gen <- MWC.create
bss1mB <- forM (replicate 10 1000000) $ randomByteString gen
encodeFile "data" bss1mB
["hask"] -> do
bss <- decodeFile "data"
bench Data.Digest.Pure.CRC32.crc32 bss
["c"] -> do
bss <- decodeFile "data"
bench Data.Digest.CRC32.crc32 bss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment