Permalink
Browse files

Add a simple performance benchmark.

  • Loading branch information...
1 parent 2d88e72 commit ff580d16ebb43a929e6b432be675a299bb1cd2e2 @bos committed May 23, 2011
Showing with 86 additions and 2 deletions.
  1. +1 −0 .hgignore
  2. +3 −0 Data/Text/Format/Params.hs
  3. +70 −0 tests/Benchmarks.hs
  4. +9 −0 tests/Makefile
  5. +3 −2 text-format.cabal
View
@@ -1,4 +1,5 @@
^(?:cabal-dev|dist)$
+^tests/bm$
\.(?:aux|eventlog|h[ip]|log|[oa]|orig|prof|ps|rej|swp)$
~$
syntax: glob
@@ -23,6 +23,9 @@ import Data.Text.Lazy.Builder
class Params ps where
buildParams :: ps -> [Builder]
+instance Params () where
+ buildParams _ = []
+
instance (Buildable a) => Params (Only a) where
buildParams (Only a) = [build a]
View
@@ -0,0 +1,70 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+import Criterion.Main
+import Data.Text.Format
+import qualified Data.Text as T
+import qualified Data.Text.Lazy as L
+
+main = defaultMain [
+ bgroup "arity" [
+ bench "0" $ nf (format "hi") ()
+ , bench "1" $ nf (format "hi {}") (Only $ T.pack "mom")
+ , bench "2" $ nf (format "hi {}, how are {}")
+ (T.pack "mom", T.pack "you")
+ , bench "3" $ nf (format "hi {}, how are {} keeping {}")
+ (T.pack "mom", T.pack "you", T.pack "now")
+ , bench "4" $ nf (format "hi {}, {} - how are {} keeping {}")
+ (T.pack "mom", T.pack "hey", T.pack "you", T.pack "now")
+ ]
+ , bgroup "types" [
+ bench "unit" $ nf (format "hi") ()
+ , bgroup "int" [
+ bench "small" $ nf (format "hi {}") (Only (1::Int))
+ , bench "medium" $ nf (format "hi {}") (Only (1234::Int))
+ , bench "large" $ nf (format "hi {}") (Only (0x7fffffff::Int))
+ ]
+ , bgroup "float" [
+ bgroup "slow" [
+ bench "small" $ nf (format "hi {}") (Only (1::Float))
+ , bench "medium" $ nf (format "hi {}") (Only (pi::Float))
+ , bench "large" $ nf (format "hi {}") (Only (pi*1e37::Float))
+ ]
+ , bgroup "fast" [
+ bench "small" $ nf (format "hi {}") (Only (1::Fast Float))
+ , bench "medium" $ nf (format "hi {}") (Only (pi::Fast Float))
+ , bench "large" $ nf (format "hi {}") (Only (pi*1e37::Fast Float))
+ ]
+ ]
+ , bgroup "double" [
+ bgroup "slow" [
+ bench "small" $ nf (format "hi {}") (Only (1::Double))
+ , bench "medium" $ nf (format "hi {}") (Only (pi::Double))
+ , bench "large" $ nf (format "hi {}") (Only (pi*1e37::Double))
+ ]
+ , bgroup "fast" [
+ bench "small" $ nf (format "hi {}") (Only (1::Fast Double))
+ , bench "medium" $ nf (format "hi {}") (Only (pi::Fast Double))
+ , bench "large" $ nf (format "hi {}") (Only (pi*1e37::Fast Double))
+ ]
+ ]
+ , bgroup "string" [
+ bench "small" $ nf (format "hi {}") (Only ("mom" :: String))
+ , bench "medium" $ nf (format "hi {}")
+ (Only . concat . replicate 64 $ ("mom" :: String))
+ , bench "large" $ nf (format "hi {}")
+ (Only . concat . replicate 1024 $ ("mom" :: String))
+ ]
+ , bgroup "text" [
+ bench "small" $ nf (format "hi {}") (Only (T.pack "mom"))
+ , bench "medium" $ nf (format "hi {}") (Only (T.replicate 64 "mom"))
+ , bench "large" $ nf (format "hi {}") (Only (T.replicate 1024 "mom"))
+ ]
+ , bgroup "lazytext" [
+ bench "small" $ nf (format "hi {}") (Only (L.pack "mom"))
+ , bench "medium" $ nf (format "hi {}")
+ (Only . L.fromChunks . replicate 64 $ "mom")
+ , bench "large" $ nf (format "hi {}")
+ (Only . L.fromChunks . replicate 1024 $ "mom")
+ ]
+ ]
+ ]
View
@@ -0,0 +1,9 @@
+ghc := ghc
+
+all: bm
+
+bm: Benchmarks.hs
+ $(ghc) -rtsopts -O -o $@ $<
+
+clean:
+ -rm -f bm *.hi *.o *.hp
View
@@ -11,13 +11,14 @@ stability: experimental
tested-with: GHC == 7.0.3
synopsis: Text formatting
cabal-version: >= 1.8
-build-type: Simple
+build-type: Simple
description:
A text formatting library optimized for ease of use and high
performance.
extra-source-files:
README.markdown
+ tests/Benchmarks.hs
flag developer
description: operate in developer mode
@@ -52,7 +53,7 @@ library
ghc-options: -Werror
ghc-prof-options: -auto-all
- ghc-options: -Wall
+ ghc-options: -Wall
cpp-options: -DINTEGER_GMP

0 comments on commit ff580d1

Please sign in to comment.