Created
May 23, 2014 20:31
-
-
Save wunki/9361c2d0172036bfe53b 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
module Main where | |
import System.Environment (getArgs) | |
import qualified Data.ByteString.Lazy as L | |
import Data.Word | |
{- | |
- Test to see how fast we can process an 1GB binary file. | |
- From the blog post: http://jvns.ca/blog/2014/05/12/computers-are-fast/ | |
- | |
-} | |
-- Result: stack space overflow... | |
addBytes :: [Word8] -> Word8 | |
addBytes [] = 0 | |
addBytes (x:xs) = x + addBytes xs | |
-- Result: stack space overflow... | |
addBytes' :: [Word8] -> Word8 | |
addBytes' = foldr (+) 0 | |
-- Result: 18 seconds | |
addBytes'' :: [Word8] -> Word8 | |
addBytes'' = sum | |
main :: IO () | |
main = do | |
args <- getArgs | |
let filename = head args | |
fc <- L.readFile filename | |
print $ addBytes'' $ L.unpack fc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment