]> Git — Sourcephile - haskell/symantic-parser.git/blob - benchmarks/Brainfuck.hs
build: update nix input `nixpkgs`
[haskell/symantic-parser.git] / benchmarks / Brainfuck.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE TypeApplications #-}
3 {-# LANGUAGE TypeFamilies #-}
4 {-# OPTIONS_GHC -Wno-missing-signatures #-}
5 module Brainfuck where
6
7 --import qualified Data.Text.Lazy as TL
8 import Control.Monad ((=<<))
9 import Criterion.Main (Benchmark, bench, bgroup, env, nf)
10 import Data.Function (($))
11 import Data.Semigroup (Semigroup(..))
12 import qualified Data.Attoparsec.ByteString as AP.ByteString
13 import qualified Data.Attoparsec.Text as AP.Text
14 import qualified Data.ByteString as BS
15 import qualified Data.ByteString.Lazy as BSL
16 import qualified Data.List as List
17 import qualified Data.Text.IO as Text
18 import qualified System.IO as IO
19
20 import qualified Parsers.Brainfuck.Attoparsec as AP.Brainfuck
21 import qualified Parsers.Brainfuck.Handrolled as HR.Brainfuck
22 import qualified Parsers.Brainfuck.SymanticParser as SP.Brainfuck
23 import Paths_symantic_parser
24
25 inputPath inputName = getDataFileName ("parsers/Parsers/Brainfuck/inputs/"<>inputName<>".bf")
26 benchBrainfuck inputName =
27 [ bgroup "Text"
28 [ env (Text.readFile =<< inputPath inputName) $ \inp ->
29 bgroup inputName
30 [ bench "SymanticParser" $
31 nf SP.Brainfuck.parserText inp
32 , bench "Attoparsec" $
33 nf (AP.Text.parse AP.Brainfuck.parser) inp
34 , bench "Handrolled" $
35 nf HR.Brainfuck.parser inp
36 ]
37 ]
38 , bgroup "String"
39 [ env (IO.readFile =<< inputPath inputName) $ \inp ->
40 bgroup inputName
41 [ bench "SymanticParser" $
42 nf SP.Brainfuck.parserString inp
43 ]
44 ]
45 , bgroup "ByteString"
46 [ env (BS.readFile =<< inputPath inputName) $ \inp ->
47 bgroup inputName
48 [ bench "SymanticParser" $
49 nf SP.Brainfuck.parserByteString inp
50 , bench "Attoparsec" $
51 nf (AP.ByteString.parse AP.Brainfuck.parser) inp
52 , bench "Handrolled" $
53 nf HR.Brainfuck.parser inp
54 ]
55 ]
56 , bgroup "ByteStringLazy"
57 [ env (BSL.readFile =<< inputPath inputName) $ \inp ->
58 bgroup inputName
59 [ bench "SymanticParser" $
60 nf SP.Brainfuck.parserByteStringLazy inp
61 ]
62 ]
63 ]
64
65 benchmark :: Benchmark
66 benchmark = bgroup "Brainfuck" $ List.concat
67 [ benchBrainfuck "compiler"
68 , benchBrainfuck "hanoi"
69 ]