on master help sign in
Transplant UTF-8 decoding benchmarks as of 44d20dca8f35
6a4ea93Bryan O'Sullivan
tests/benchmarks/src/Data/Text/Benchmarks.hs+5−1
@@ -39,7 +39,11 @@ benchmarks = do
39 -- Traditional benchmarks
39 -- Traditional benchmarks
40 bs <- sequence
40 bs <- sequence
41 [ Builder.benchmark
41 [ Builder.benchmark
42− , DecodeUtf8.benchmark (tf "russian.txt")
42+ , DecodeUtf8.benchmark "html" (tf "libya-chinese.html")
43+ , DecodeUtf8.benchmark "xml" (tf "yiwiki.xml")
44+ , DecodeUtf8.benchmark "ascii" (tf "ascii.txt")
45+ , DecodeUtf8.benchmark "russian" (tf "russian.txt")
46+ , DecodeUtf8.benchmark "japanese" (tf "japanese.txt")
43 , EncodeUtf8.benchmark "επανάληψη 竺法蘭共譯"
47 , EncodeUtf8.benchmark "επανάληψη 竺法蘭共譯"
44 , Equality.benchmark (tf "japanese.txt")
48 , Equality.benchmark (tf "japanese.txt")
45 , FileRead.benchmark (tf "russian.txt")
49 , FileRead.benchmark (tf "russian.txt")
tests/benchmarks/src/Data/Text/Benchmarks/DecodeUtf8.hs+19−3
@@ -1,3 +1,5 @@
1+{-# LANGUAGE ForeignFunctionInterface #-}
2+
1-- | Test decoding of UTF-8
3-- | Test decoding of UTF-8
2--
4--
3-- Tested in this benchmark:
5-- Tested in this benchmark:
@@ -16,7 +18,13 @@ module Data.Text.Benchmarks.DecodeUtf8
16 ( benchmark
18 ( benchmark
17 ) where
19 ) where
18
20
21+import Foreign.C.Types (CInt, CSize)
22+import Data.ByteString.Internal (ByteString(..))
23+import Foreign.Ptr (Ptr, plusPtr)
24+import Foreign.ForeignPtr (withForeignPtr)
25+import Data.Word (Word8)
26+import qualified Criterion as C
19−import Criterion (Benchmark, bgroup, bench, nf)
27+import Criterion (Benchmark, bgroup, nf)
20import qualified Codec.Binary.UTF8.Generic as U8
28import qualified Codec.Binary.UTF8.Generic as U8
21import qualified Data.ByteString as B
29import qualified Data.ByteString as B
22import qualified Data.ByteString.Lazy as BL
30import qualified Data.ByteString.Lazy as BL
@@ -25,12 +33,14 @@ import qualified Data.Text.Encoding as T
25import qualified Data.Text.Lazy as TL
33import qualified Data.Text.Lazy as TL
26import qualified Data.Text.Lazy.Encoding as TL
34import qualified Data.Text.Lazy.Encoding as TL
27
35
28−benchmark :: FilePath -> IO Benchmark
36+benchmark :: String -> FilePath -> IO Benchmark
29−benchmark fp = do
37+benchmark kind fp = do
30 bs <- B.readFile fp
38 bs <- B.readFile fp
31 lbs <- BL.readFile fp
39 lbs <- BL.readFile fp
40+ let bench name = C.bench (name ++ "+" ++ kind)
32 return $ bgroup "DecodeUtf8"
41 return $ bgroup "DecodeUtf8"
33 [ bench "Strict" $ nf T.decodeUtf8 bs
42 [ bench "Strict" $ nf T.decodeUtf8 bs
43+ , bench "IConv" $ iconv bs
34 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
44 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
35 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
45 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
36 , bench "Lazy" $ nf TL.decodeUtf8 lbs
46 , bench "Lazy" $ nf TL.decodeUtf8 lbs
@@ -41,3 +51,9 @@ benchmark fp = do
41 , bench "LazyStringUtf8" $ nf U8.toString lbs
51 , bench "LazyStringUtf8" $ nf U8.toString lbs
42 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
52 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
43 ]
53 ]
54+
55+iconv :: ByteString -> IO CInt
56+iconv (PS fp off len) = withForeignPtr fp $ \ptr ->
57+ time_iconv (ptr `plusPtr` off) (fromIntegral len)
58+
59+foreign import ccall unsafe time_iconv :: Ptr Word8 -> CSize -> IO CInt
tests/benchmarks/text-benchmarks.cabal+1
@@ -18,6 +18,7 @@ cabal-version: >=1.2
18executable text-benchmarks
18executable text-benchmarks
19 hs-source-dirs: src ../..
19 hs-source-dirs: src ../..
20 c-sources: ../../cbits/cbits.c
20 c-sources: ../../cbits/cbits.c
21+ cbits/time_iconv.c
21 main-is: Data/Text/Benchmarks.hs
22 main-is: Data/Text/Benchmarks.hs
22 ghc-options: -Wall -O2
23 ghc-options: -Wall -O2
23 cpp-options: -DHAVE_DEEPSEQ
24 cpp-options: -DHAVE_DEEPSEQ