]> Git — Sourcephile - haskell/symantic-parser.git/blob - benchmarks/Brainfuck.hs
TemplateHaskell: why is PprSplice much faster than DumpSplice?
[haskell/symantic-parser.git] / benchmarks / Brainfuck.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE TypeApplications #-}
3 {-# LANGUAGE TypeFamilies #-}
4 {-# OPTIONS_GHC -Wno-missing-signatures #-}
5 -- for Symantic.Parser's TemplateHaskell
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE RankNTypes #-}
8 {-# LANGUAGE UnboxedTuples #-}
9 {-# OPTIONS_GHC -Wno-unused-matches #-}
10 {-# OPTIONS_GHC -Wno-unused-local-binds #-}
11 module Brainfuck where
12
13 --import qualified Data.Text.Lazy as TL
14 import Control.Monad ((=<<))
15 import Criterion.Main (Benchmark, bench, bgroup, env, nf)
16 import Data.Function (($))
17 import Data.Semigroup (Semigroup(..))
18 import Data.String (String)
19 import Data.Text (Text)
20 import qualified Data.Attoparsec.ByteString as AP.ByteString
21 import qualified Data.Attoparsec.Text as AP.Text
22 import qualified Data.ByteString as BS
23 import qualified Data.ByteString.Lazy as BSL
24 import qualified Data.List as List
25 import qualified Data.Text.IO as Text
26 import qualified System.IO as IO
27
28 import qualified Symantic.Parser as SP
29 import qualified Parsers.Brainfuck.Attoparsec as AP.Brainfuck
30 import qualified Parsers.Brainfuck.Handrolled as HR.Brainfuck
31 import qualified Parsers.Brainfuck.SymanticParser.Grammar as SP.Brainfuck
32 import qualified Parsers.Brainfuck.SymanticParser.AutoSplice as SP.Brainfuck.AutoSplice
33 import qualified Parsers.Brainfuck.SymanticParser.DumpSplice as SP.Brainfuck.DumpSplice
34 import qualified Parsers.Brainfuck.SymanticParser.PprSplice as SP.Brainfuck.PprSplice
35 import Paths_symantic_parser
36
37 inputPath inputName = getDataFileName ("parsers/Parsers/Brainfuck/inputs/"<>inputName<>".bf")
38 benchBrainfuck inputName =
39 [ bgroup "Text"
40 [ env (Text.readFile =<< inputPath inputName) $ \inp ->
41 bgroup inputName
42 [ bench "SymanticParser" $
43 nf $$(SP.runParser @Text SP.Brainfuck.grammar) inp
44 , bench "Attoparsec" $
45 nf (AP.Text.parse AP.Brainfuck.parser) inp
46 , bench "Handrolled" $
47 nf HR.Brainfuck.parser inp
48 ]
49 ]
50 , bgroup "String"
51 [ env (IO.readFile =<< inputPath inputName) $ \inp ->
52 bgroup inputName
53 [ bench "SymanticParser" $
54 nf $$(SP.runParser @String SP.Brainfuck.grammar) inp
55 ]
56 ]
57 , bgroup "ByteString"
58 [ env (BS.readFile =<< inputPath inputName) $ \inp ->
59 bgroup inputName
60 [ bench "SymanticParser.PprSplice" $
61 nf SP.Brainfuck.PprSplice.parserByteString inp
62 , bench "SymanticParser.DumpSplice" $
63 nf SP.Brainfuck.DumpSplice.parserByteString inp
64 , bench "SymanticParser.AutoSplice" $
65 nf SP.Brainfuck.AutoSplice.parserByteString inp
66 , bench "SymanticParser" $
67 nf $$(SP.runParser @BS.ByteString SP.Brainfuck.grammar) inp
68 , bench "Attoparsec" $
69 nf (AP.ByteString.parse AP.Brainfuck.parser) inp
70 , bench "Handrolled" $
71 nf HR.Brainfuck.parser inp
72 ]
73 ]
74 , bgroup "ByteStringLazy"
75 [ env (BSL.readFile =<< inputPath inputName) $ \inp ->
76 bgroup inputName
77 [ bench "SymanticParser" $
78 nf $$(SP.runParser @BSL.ByteString SP.Brainfuck.grammar) inp
79 ]
80 ]
81 ]
82
83 benchmark :: Benchmark
84 benchmark = bgroup "Brainfuck" $ List.concat
85 [ benchBrainfuck "helloworld"
86 , benchBrainfuck "compiler"
87 , benchBrainfuck "hanoi"
88 ]
89
90 init =
91 SP.Brainfuck.PprSplice.dumpSplice