Create a gist now

Instantly share code, notes, and snippets.

@bitonic /crc32.hs
Last active Aug 29, 2015

What would you like to do?
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