@@ -0,0 +1,177 @@
1+{-# LANGUAGE MagicHash #-}
2+
3+import qualified Data.ByteString.Char8 as B8
4+import Control.Exception (evaluate)
5+import Control.DeepSeq (NFData(..))
6+import Criterion.Main
7+import Data.Char
8+import qualified Codec.Binary.UTF8.Generic as UTF8
9+import qualified Data.Text as T
10+import qualified Data.List as L
11+import Data.Text.Encoding
12+import qualified Criterion.MultiMap as M
13+import Criterion.Config
14+import GHC.Base
15+
16+
17+myConfig
18+ | False = defaultConfig {
19+ -- Always display an 800x600 window.
20+ cfgPlot = M.singleton KernelDensity (Window 800 600)
21+ }
22+ | otherwise = defaultConfig
23+
24+instance NFData B8.ByteString
25+
26+main = do
27+ s0 <- B8.readFile "text/test/russian.txt"
28+ let !s0l = B8.length s0
29+ t0 = decodeUtf8 s0
30+ !t0l = T.length t0
31+ l0 = UTF8.toString s0
32+ !l0l = L.length l0
33+ s1 = B8.map toUpper s0
34+ !s1l = B8.length s1
35+ t1 = T.toUpper t0
36+ !t1l = T.length t1
37+ l1 = L.map toUpper l0
38+ !l1l = L.length l1
39+ sl = B8.lines s0
40+ tl = T.lines t0
41+ ll = L.lines l0
42+ evaluate (rnf (sl,tl,ll))
43+ defaultMainWith myConfig [
44+ bgroup "append" [
45+ bench "text" $ nf (T.append t1) t0
46+ , bench "b8" $ nf (B8.append s1) s0
47+ , bench "list" $ nf ((++) l1) l0
48+ ],
49+ bgroup "concat" [
50+ bench "text" $ nf T.concat tl
51+ , bench "b8" $ nf B8.concat sl
52+ , bench "list" $ nf L.concat ll
53+ ],
54+ bgroup "decode" [
55+ bench "text" $ nf decodeUtf8 s0
56+ , bench "b8" $ nf B8.unpack s0
57+ , bench "utf8-string" $ nf UTF8.toString s0
58+ ],
59+ bgroup "drop" [
60+ bench "text" $ nf (T.drop (t0l `div` 3)) t0
61+ , bench "b8" $ nf (B8.drop (s0l `div` 3)) s0
62+ , bench "list" $ nf (L.drop (l0l `div` 3)) l0
63+ ],
64+ bgroup "filter" [
65+ bench "text" $ nf (T.filter p0) t0
66+ , bench "b8" $ nf (B8.filter p0) s0
67+ , bench "list" $ nf (L.filter p0) l0
68+ ],
69+ bgroup "2filter" [
70+ bench "text" $ nf (T.filter p1 . T.filter p0) t0
71+ , bench "b8" $ nf (B8.filter p1 . B8.filter p0) s0
72+ , bench "list" $ nf (L.filter p1 . L.filter p0) l0
73+ ],
74+ bgroup "foldl'" [
75+ bench "text" $ nf (T.foldl' len 0) t0
76+ , bench "b8" $ nf (B8.foldl' len 0) s0
77+ , bench "list" $ nf (L.foldl' len 0) l0
78+ ],
79+ bgroup "foldr" [
80+ bench "text" $ nf (L.length . T.foldr (:) []) t0
81+ , bench "b8" $ nf (L.length . B8.foldr (:) []) s0
82+ , bench "list" $ nf (L.length . L.foldr (:) []) l0
83+ ],
84+ bgroup "isInfixOf" [
85+ bench "text" $ nf (T.isInfixOf (T.pack w)) t0
86+ , bench "b8" $ nf (B8.isInfixOf (B8.pack w)) s0
87+ , bench "list" $ nf (L.isInfixOf w) l0
88+ ],
89+ bgroup "last" [
90+ bench "text" $ nf T.last t0
91+ , bench "b8" $ nf B8.last s0
92+ , bench "list" $ nf L.last l0
93+ ],
94+ bgroup "map" [
95+ bench "text" $ nf (T.map f) t0
96+ , bench "b8" $ nf (B8.map f) s0
97+ , bench "list" $ nf (L.map f) l0
98+ ],
99+ bgroup "2map" [
100+ bench "text" $ nf (T.map f . T.map f) t0
101+ , bench "b8" $ nf (B8.map f . B8.map f) s0
102+ , bench "list" $ nf (L.map f . L.map f) l0
103+ ],
104+ bgroup "reverse" [
105+ bench "text" $ nf T.reverse t0
106+ , bench "b8" $ nf B8.reverse s0
107+ , bench "list" $ nf L.reverse l0
108+ ],
109+ bgroup "take" [
110+ bench "text" $ nf (T.take (t0l `div` 3)) t0
111+ , bench "b8" $ nf (B8.take (s0l `div` 3)) s0
112+ , bench "list" $ nf (L.take (l0l `div` 3)) l0
113+ ],
114+ bgroup "words" [
115+ bench "text" $ nf T.words t0
116+ , bench "b8" $ nf B8.words s0
117+ , bench "list" $ nf L.words l0
118+ ],
119+ bgroup "zipWith" [
120+ bench "text" $ nf (T.zipWith min t1) t0
121+ , bench "b8" $ nf (B8.zipWith min s1) s0
122+ , bench "list" $ nf (L.zipWith min l1) l0
123+ ],
124+ bgroup "length" [
125+ bgroup "decode" [
126+ bench "text" $ nf (T.length . decodeUtf8) s0
127+ , bench "b8" $ nf (L.length . B8.unpack) s0
128+ , bench "utf8-string" $ nf (L.length . UTF8.toString) s0
129+ ],
130+ bgroup "drop" [
131+ bench "text" $ nf (T.length . T.drop (t0l `div` 3)) t0
132+ , bench "b8" $ nf (B8.length . B8.drop (s0l `div` 3)) s0
133+ , bench "list" $ nf (L.length . L.drop (l0l `div` 3)) l0
134+ ],
135+ bgroup "filter" [
136+ bench "text" $ nf (T.length . T.filter p0) t0
137+ , bench "b8" $ nf (B8.length . B8.filter p0) s0
138+ , bench "list" $ nf (L.length . L.filter p0) l0
139+ ],
140+ bgroup "2filter" [
141+ bench "text" $ nf (T.length . T.filter p1 . T.filter p0) t0
142+ , bench "b8" $ nf (B8.length . B8.filter p1 . B8.filter p0) s0
143+ , bench "list" $ nf (L.length . L.filter p1 . L.filter p0) l0
144+ ],
145+ bgroup "map" [
146+ bench "text" $ nf (T.length . T.map f) t0
147+ , bench "b8" $ nf (B8.length . B8.map f) s0
148+ , bench "list" $ nf (L.length . L.map f) l0
149+ ],
150+ bgroup "2map" [
151+ bench "text" $ nf (T.length . T.map f . T.map f) t0
152+ , bench "b8" $ nf (B8.length . B8.map f . B8.map f) s0
153+ , bench "list" $ nf (L.length . L.map f . L.map f) l0
154+ ],
155+ bgroup "take" [
156+ bench "text" $ nf (T.length . T.take (t0l `div` 3)) t0
157+ , bench "b8" $ nf (B8.length . B8.take (s0l `div` 3)) s0
158+ , bench "list" $ nf (L.length . L.take (l0l `div` 3)) l0
159+ ],
160+ bgroup "words" [
161+ bench "text" $ nf (L.length . T.words) t0
162+ , bench "b8" $ nf (L.length . B8.words) s0
163+ , bench "list" $ nf (L.length . L.words) l0
164+ ],
165+ bgroup "zipWith" [
166+ bench "text" $ nf (T.length . T.zipWith min t1) t0
167+ , bench "b8" $ nf (L.length . B8.zipWith min s1) s0
168+ , bench "list" $ nf (L.length . L.zipWith min l1) l0
169+ ]
170+ ]
171+ ]
172+ where
173+ p0 = (== 'й')
174+ p1 = (/= 'д')
175+ w = "право"
176+ f (C# c#) = C# (chr# (ord# c# +# 1#))
177+ len l _ = l + (1::Int)