Created
June 5, 2011 18:00
-
-
Save tsuraan/1009232 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE BangPatterns #-} | |
import Data.Digest.Adler32 (adler32) | |
import Data.ByteString (ByteString) | |
import qualified Data.ByteString as BS | |
import Criterion.Main | |
import System.IO | |
import Data.Word | |
data PairS a b = PS !a !b | |
updateHash :: Word32 -> Word8 -> Word32 -> Word8 -> Word32 | |
updateHash _winlen old8 !hash word8 = hash + (fromIntegral $ old8 + word8) | |
{- actual adler32 sliding window code goes here -} | |
updateHash' :: ByteString -> Word32 -> Int -> Word32 -> Word8 -> Word32 | |
updateHash' bs winlen oldpos = | |
updateHash winlen old | |
where | |
old = {-# SCC "BS.index" #-} (BS.index bs oldpos) | |
runWindow :: Word32 -> ByteString -> Word32 | |
runWindow winlen bs = | |
let (inits, rest) = BS.splitAt (fromIntegral winlen) bs | |
updatefn :: PairS Int Word32 -> Word8 -> PairS Int Word32 | |
updatefn = \(PS oldp hash) word -> | |
PS (oldp + 1) (updateHash' bs winlen oldp hash word) | |
PS _ w = BS.foldl updatefn (PS 0 (adler32 inits)) rest | |
in w | |
main = do | |
bytes <- withFile "/dev/urandom" ReadMode ((flip BS.hGet) 16777216) | |
defaultMain [ bgroup "adler32" [ bench "works?" $ nf (runWindow 8) bytes ]] | |
-- return $! runWindow 8 bytes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment