on master help sign in
Transplant UTF-8 decoding benchmarks as of 44d20dca8f35
commit6a4ea93a408bbfb408311211fb903826eff9f53b authorBryan O'Sullivan <bos@serpentine.com> dateSun 10 Jul 2011, 13:33 (15 years ago) parent9dec3d6
3 files changed+25−4
tests/benchmarks/src/Data/Text/Benchmarks.hs+5−1 tests/benchmarks/src/Data/Text/Benchmarks/DecodeUtf8.hs+19−3 tests/benchmarks/text-benchmarks.cabal+1
tests/benchmarks/src/Data/Text/Benchmarks.hs+5−1
@@ -39,7 +39,11 @@ benchmarks = do
3939 -- Traditional benchmarks
4040 bs <- sequence
4141 [ 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")
4347 , EncodeUtf8.benchmark "επανάληψη 竺法蘭共譯"
4448 , Equality.benchmark (tf "japanese.txt")
4549 , FileRead.benchmark (tf "russian.txt")
tests/benchmarks/src/Data/Text/Benchmarks/DecodeUtf8.hs+19−3
@@ -1,3 +1,5 @@
1+{-# LANGUAGE ForeignFunctionInterface #-}
2+
13-- | Test decoding of UTF-8
24--
35-- Tested in this benchmark:
@@ -16,7 +18,13 @@ module Data.Text.Benchmarks.DecodeUtf8
1618 ( benchmark
1719 ) where
1820
19−import Criterion (Benchmark, bgroup, bench, nf)
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
27+import Criterion (Benchmark, bgroup, nf)
2028import qualified Codec.Binary.UTF8.Generic as U8
2129import qualified Data.ByteString as B
2230import qualified Data.ByteString.Lazy as BL
@@ -25,12 +33,14 @@ import qualified Data.Text.Encoding as T
2533import qualified Data.Text.Lazy as TL
2634import qualified Data.Text.Lazy.Encoding as TL
2735
28−benchmark :: FilePath -> IO Benchmark
29−benchmark fp = do
36+benchmark :: String -> FilePath -> IO Benchmark
37+benchmark kind fp = do
3038 bs <- B.readFile fp
3139 lbs <- BL.readFile fp
40+ let bench name = C.bench (name ++ "+" ++ kind)
3241 return $ bgroup "DecodeUtf8"
3342 [ bench "Strict" $ nf T.decodeUtf8 bs
43+ , bench "IConv" $ iconv bs
3444 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
3545 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
3646 , bench "Lazy" $ nf TL.decodeUtf8 lbs
@@ -41,3 +51,9 @@ benchmark fp = do
4151 , bench "LazyStringUtf8" $ nf U8.toString lbs
4252 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
4353 ]
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
1818executable text-benchmarks
1919 hs-source-dirs: src ../..
2020 c-sources: ../../cbits/cbits.c
21+ cbits/time_iconv.c
2122 main-is: Data/Text/Benchmarks.hs
2223 ghc-options: -Wall -O2
2324 cpp-options: -DHAVE_DEEPSEQ