Created
April 25, 2012 07:44
-
-
Save meiersi/2487861 to your computer and use it in GitHub Desktop.
Decimal encoding speed
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
-- | | |
-- Copyright : (c) 2012 Simon Meier | |
-- License : BSD3-style (see LICENSE) | |
-- | |
-- Maintainer : Simon Meier <iridcode@gmail.com> | |
-- Stability : experimental | |
-- Portability : tested on GHC only | |
-- | |
-- Benchmark decimal encoding speed of 10 million integers. | |
-- | |
-- The builder implementation uses the following repository: | |
-- | |
-- https://github.com/meiersi/bytestring-builder | |
-- | |
module Main (main) where | |
import Control.Monad (forM_) | |
import Data.Foldable (foldMap) | |
import Data.Monoid (mappend) | |
import qualified Data.ByteString.Lazy as L | |
import Data.ByteString.Lazy.Builder | |
import Data.ByteString.Lazy.Builder.ASCII | |
import qualified Data.ByteString.Lazy.Builder.BasicEncoding as E | |
import System.IO | |
import System.Environment | |
main :: IO () | |
main = do | |
h <- openFile "/dev/null" WriteMode | |
let useBuilder = L.hPut h . toLazyByteString | |
args <- getArgs | |
case args of | |
"original":_ -> | |
forM_ [1..10000000 :: Int] $ \j -> hPutStrLn h (show j) | |
"unlines": _ -> | |
hPutStr h $ unlines $ map show $ [1 :: Int .. 10000000] | |
"builder1":_ -> useBuilder $ | |
foldMap ((charUtf8 '\n' `mappend`) . intDec) [1..10000000] | |
"builder2":_ -> useBuilder $ | |
E.encodeListWithB | |
((\x -> (x, '\n')) E.>$< E.intDec `E.pairB` E.charUtf8) | |
[1..10000000] | |
"builder3":_ -> useBuilder $ | |
E.encodeUnfoldrWithB | |
((\x -> (x, '\n')) E.>$< E.intDec `E.pairB` E.charUtf8) | |
(\x -> if x <= 10000000 then Just (x, x + 1) else Nothing) 1 | |
_ -> error "Please specify which function to use: original, unlines, builder1, builder2, or builder3." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment