@@ -1,3 +1,5 @@
1+{-# LANGUAGE ForeignFunctionInterface #-}
1-- | Test decoding of UTF-8
3-- | Test decoding of UTF-8
3-- Tested in this benchmark:
5-- Tested in this benchmark:
@@ -16,7 +18,13 @@ module Data.Text.Benchmarks.DecodeUtf8
16 ( benchmark
18 ( benchmark
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
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
55+iconv :: ByteString -> IO CInt
56+iconv (PS fp off len) = withForeignPtr fp $ \ptr ->
57+ time_iconv (ptr `plusPtr` off) (fromIntegral len)
59+foreign import ccall unsafe time_iconv :: Ptr Word8 -> CSize -> IO CInt