-
Notifications
You must be signed in to change notification settings - Fork 7
/
Microbench.hs
33 lines (27 loc) · 1.01 KB
/
Microbench.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE OverloadedStrings #-}
import GHC.Exts
import Data.Char
import qualified Data.Text as T
import Data.Attoparsec.Text
import Criterion.Main
import Prelude hiding (take, takeWhile)
isAsciiAlpha1 :: Char -> Bool
isAsciiAlpha1 c = isAsciiLower c || isAsciiUpper c
isAsciiAlpha2 :: Char -> Bool
isAsciiAlpha2 (C# c) =
tagToEnum# ( ((c `geChar#` 'A'#) `andI#` (c `leChar#` 'Z'#))
`orI#` ((c `geChar#` 'a'#) `andI#` (c `leChar#` 'z'#)) )
main :: IO ()
main = defaultMain
[ bench "Text isAsciiAlpha1"
$ whnf (T.length . T.takeWhile isAsciiAlpha1) testString
, bench "Text isAsciiAlpha2"
$ whnf (T.length . T.takeWhile isAsciiAlpha2) testString
, bench "Attoparsec isAsciiAlpha1"
$ whnf (parseOnly (T.length <$> takeWhile isAsciiAlpha1)) testString
, bench "Attoparsec isAsciiAlpha2"
$ whnf (parseOnly (T.length <$> takeWhile isAsciiAlpha2)) testString
]
testString :: T.Text
testString = T.replicate 10 "helloworldthisisarelativelylongstring"