Skip to content

Commit 0812684

Browse files
committed
add benchmark showing minor benifit to previous commit
1 parent 2568684 commit 0812684

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

benchmarks/Benchmarks.hs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Benchmarks where
2+
3+
import Database.PostgreSQL.Simple.SqlQQ.Interpolated
4+
import Database.PostgreSQL.Simple.SqlQQ.Interpolated.Parser
5+
6+
import Data.List (foldl')
7+
import Criterion
8+
import Criterion.Main
9+
import Language.Haskell.TH
10+
11+
main = defaultMain
12+
[ bench "combineParts foldl' length 2" $
13+
nf combinePartsL' sql2
14+
, bench "combineParts foldl' length 100" $
15+
nf combinePartsL' sql100
16+
, bench "combineParts foldr length 2" $ nf combinePartsR sql2
17+
, bench "combineParts foldr length 100" $ nf combinePartsR sql100
18+
]
19+
20+
21+
sql2 :: [Either String Int]
22+
sql2 =
23+
[ Left "SELECT field FROM table WHERE name = "
24+
, Right 1
25+
, Left " LIMIT "
26+
, Right 2
27+
]
28+
29+
sql100 :: [Either String Int]
30+
sql100 = concat [ [Left " ", Right n ] | n <- [1..100]]
31+
32+
-- | The same as combineParts in the library, using foldl'
33+
combinePartsL' :: [Either String Int] -> (String, [Int])
34+
combinePartsL' = foldl' step ("", [])
35+
where
36+
step (s, exprs) subExpr = case subExpr of
37+
Left str -> (s <> str, exprs)
38+
Right e -> (s <> "?", exprs <> [e]) -- TODO: Make this not slow
39+
40+
combinePartsR :: [Either String Int] -> (String, [Int])
41+
combinePartsR = foldr step ("", [])
42+
where step subExpr (s, exprs) = case subExpr of
43+
Left str -> (str <> s, exprs)
44+
Right e -> ("?" <> s, e : exprs)

postgresql-simple-interpolate.cabal

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,14 @@ library
2929
postgresql-simple >= 0.1,
3030
template-haskell
3131
ghc-options: -Wall -O2
32+
33+
benchmark criterion
34+
hs-source-dirs: benchmarks
35+
main-is: Benchmarks.hs
36+
ghc-options: -O2 -Wall -rtsopts
37+
build-depends: postgresql-simple-interpolate
38+
, base
39+
, criterion
40+
default-language: Haskell2010
41+
type: exitcode-stdio-1.0
42+

0 commit comments

Comments
 (0)