Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add benchmarks for name-based type conversion

  • Loading branch information...
commit e1c1428e9d5de33e5e7a1bdb8e43680ae6595a69 1 parent 646f0a5
@tibbe authored
Showing with 34 additions and 5 deletions.
  1. +34 −5 benchmarks/Benchmarks.hs
View
39 benchmarks/Benchmarks.hs
@@ -1,6 +1,8 @@
-{-# LANGUAGE BangPatterns #-}
+{-# LANGUAGE BangPatterns, FlexibleInstances, OverloadedStrings,
+ TypeSynonymInstances #-}
module Main ( main ) where
+import Control.Applicative
import Criterion.Main
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
@@ -12,16 +14,43 @@ import Data.Ceason
type President = (Int, Text, ByteString, ByteString, ByteString, Text, Text)
+instance FromNamedRecord President where
+ parseNamedRecord m = (,,,,,,) <$>
+ m .: "Presidency" <*>
+ m .: "President" <*>
+ m .: "Wikipedia Entry" <*>
+ m .: "Took office" <*>
+ m .: "Left office" <*>
+ m .: "Party" <*>
+ m .: "Home State"
+
+
+fromStrict s = BL.fromChunks [s]
+
main :: IO ()
main = do
- !csvData <- (BL.fromChunks . \ x -> [x]) `fmap` B.readFile "benchmarks/presidents.csv"
+ !csvData <- fromStrict `fmap` B.readFile "benchmarks/presidents.csv"
+ !csvDataH <- fromStrict `fmap` B.readFile
+ "benchmarks/presidents_with_header.csv"
defaultMain [
- bench "presidents/without conversion" $ whnf idDecode csvData
- , bench "presidents/with conversion" $ whnf decodePresidents csvData
+ bgroup "indexed" [
+ bench "presidents/without conversion" $ whnf idDecode csvData
+ , bench "presidents/with conversion" $ whnf decodePresidents csvData
+ ]
+ , bgroup "named" [
+ bench "presidents/without conversion" $ whnf idDecodeN csvDataH
+ , bench "presidents/with conversion" $ whnf decodePresidentsN csvDataH
+ ]
]
where
decodePresidents :: BL.ByteString -> Either String (Vector President)
decodePresidents = decode
- idDecode ::BL.ByteString -> Either String (Vector (Vector B.ByteString))
+ decodePresidentsN :: BL.ByteString -> Either String (Header, Vector President)
+ decodePresidentsN = decodeWithHeader
+
+ idDecode :: BL.ByteString -> Either String (Vector (Vector B.ByteString))
idDecode = decode
+
+ idDecodeN :: BL.ByteString -> Either String (Header, Vector (BSHashMap B.ByteString))
+ idDecodeN = decodeWithHeader
Please sign in to comment.
Something went wrong with that request. Please try again.