|
| 1 | +-- | # Benchmarking |
| 2 | +-- | |
| 3 | +-- | spago -x spago-dev.dhall run --main Bench.Main |
| 4 | +-- | |
| 5 | +-- | This benchmark suite is intended to guide changes to this package so that |
| 6 | +-- | we can compare the benchmarks of different commits. |
| 7 | +-- | |
| 8 | +-- | This benchmark suite also compares parsers to equivalent Regex. This |
| 9 | +-- | provides an answer to the common question “How much slower is this package |
| 10 | +-- | than Regex?” Answer: approximately 100×. The Regex benchmarks also give |
| 11 | +-- | us a rough way to calibrate benchmarks run on different platforms. |
| 12 | +module Bench.Main where |
| 13 | + |
| 14 | +import Prelude |
| 15 | + |
| 16 | +import Data.Array (fold, replicate) |
| 17 | +import Data.Either (either) |
| 18 | +import Data.List (manyRec) |
| 19 | +import Data.List.Types (List) |
| 20 | +import Data.String.Regex (Regex, regex) |
| 21 | +import Data.String.Regex as Regex |
| 22 | +import Data.String.Regex.Flags (RegexFlags(..)) |
| 23 | +import Effect (Effect) |
| 24 | +import Effect.Console (log) |
| 25 | +import Effect.Exception (throw) |
| 26 | +import Effect.Unsafe (unsafePerformEffect) |
| 27 | +import Performance.Minibench (benchWith) |
| 28 | +import Text.Parsing.Parser (Parser, runParser) |
| 29 | +import Text.Parsing.Parser.Token (digit) |
| 30 | +import Text.Parsing.Parser.String (string) |
| 31 | + |
| 32 | +string23 :: String |
| 33 | +string23 = "23" |
| 34 | +string23_2 :: String |
| 35 | +string23_2 = fold $ replicate 2 string23 |
| 36 | +string23_10000 :: String |
| 37 | +string23_10000 = fold $ replicate 10000 string23 |
| 38 | + |
| 39 | +stringSkidoo :: String |
| 40 | +stringSkidoo = "skidoo" |
| 41 | +stringSkidoo_2 :: String |
| 42 | +stringSkidoo_2 = fold $ replicate 2 stringSkidoo |
| 43 | +stringSkidoo_10000 :: String |
| 44 | +stringSkidoo_10000 = fold $ replicate 10000 stringSkidoo |
| 45 | + |
| 46 | +parse23 :: Parser String (List Char) |
| 47 | +parse23 = manyRec digit |
| 48 | + |
| 49 | +pattern23 :: Regex |
| 50 | +pattern23 = either (unsafePerformEffect <<< throw) identity $ |
| 51 | + regex "\\d" $ RegexFlags |
| 52 | + { dotAll: true |
| 53 | + , global: true |
| 54 | + , ignoreCase: false |
| 55 | + , multiline: true |
| 56 | + , sticky: false |
| 57 | + , unicode: true |
| 58 | + } |
| 59 | + |
| 60 | +parseSkidoo :: Parser String (List String) |
| 61 | +parseSkidoo = manyRec $ string "skidoo" |
| 62 | + |
| 63 | +patternSkidoo :: Regex |
| 64 | +patternSkidoo = either (unsafePerformEffect <<< throw) identity $ |
| 65 | + regex "skidoo" $ RegexFlags |
| 66 | + { dotAll: true |
| 67 | + , global: true |
| 68 | + , ignoreCase: false |
| 69 | + , multiline: true |
| 70 | + , sticky: false |
| 71 | + , unicode: true |
| 72 | + } |
| 73 | + |
| 74 | +main :: Effect Unit |
| 75 | +main = do |
| 76 | + -- log $ show $ runParser string23_2 parse23 |
| 77 | + -- log $ show $ Regex.match pattern23 string23_2 |
| 78 | + -- log $ show $ runParser stringSkidoo_2 parseSkidoo |
| 79 | + -- log $ show $ Regex.match patternSkidoo stringSkidoo_2 |
| 80 | + log "runParser parse23" |
| 81 | + benchWith 200 |
| 82 | + $ \_ -> runParser string23_10000 parse23 |
| 83 | + log "Regex.match pattern23" |
| 84 | + benchWith 200 |
| 85 | + $ \_ -> Regex.match pattern23 string23_10000 |
| 86 | + log "runParser parseSkidoo" |
| 87 | + benchWith 200 |
| 88 | + $ \_ -> runParser stringSkidoo_10000 parseSkidoo |
| 89 | + log "Regex.match patternSkidoo" |
| 90 | + benchWith 200 |
| 91 | + $ \_ -> Regex.match patternSkidoo stringSkidoo_10000 |
0 commit comments