on master help sign in
Benchmark the performance of iconv.
30a0509Bryan O'Sullivan
tests/benchmarks/cbits/time_iconv.cnew file+35
@@ -0,0 +1,35 @@
1+#include <iconv.h>
2+#include <stdlib.h>
3+#include <stdio.h>
4+#include <stdint.h>
5+
6+int time_iconv(char *srcbuf, size_t srcbufsize)
7+{
8+ uint16_t *destbuf = NULL;
9+ size_t destbufsize;
10+ static uint16_t *origdestbuf;
11+ static size_t origdestbufsize;
12+ iconv_t ic = (iconv_t) -1;
13+ int ret = 0;
14+
15+ if (ic == (iconv_t) -1) {
16+ ic = iconv_open("UTF-16LE", "UTF-8");
17+ if (ic == (iconv_t) -1) {
18+ ret = -1;
19+ goto done;
20+ }
21+ }
22+
23+ destbufsize = srcbufsize * sizeof(uint16_t);
24+ if (destbufsize > origdestbufsize) {
25+ free(origdestbuf);
26+ origdestbuf = destbuf = malloc(origdestbufsize = destbufsize);
27+ } else {
28+ destbuf = origdestbuf;
29+ }
30+
31+ iconv(ic, &srcbuf, &srcbufsize, (char**) &destbuf, &destbufsize);
32+
33+ done:
34+ return ret;
35+}
tests/benchmarks/src/Data/Text/Benchmarks/DecodeUtf8.hs+14
@@ -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,6 +18,11 @@ 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)
19import Criterion (Benchmark, bgroup, bench, nf)
26import Criterion (Benchmark, bgroup, bench, nf)
20import qualified Codec.Binary.UTF8.Generic as U8
27import qualified Codec.Binary.UTF8.Generic as U8
21import qualified Data.ByteString as B
28import qualified Data.ByteString as B
@@ -31,6 +38,7 @@ benchmark fp = do
31 lbs <- BL.readFile fp
38 lbs <- BL.readFile fp
32 return $ bgroup "DecodeUtf8"
39 return $ bgroup "DecodeUtf8"
33 [ bench "Strict" $ nf T.decodeUtf8 bs
40 [ bench "Strict" $ nf T.decodeUtf8 bs
41+ , bench "IConv" $ iconv bs
34 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
42 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
35 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
43 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
36 , bench "Lazy" $ nf TL.decodeUtf8 lbs
44 , bench "Lazy" $ nf TL.decodeUtf8 lbs
@@ -41,3 +49,9 @@ benchmark fp = do
41 , bench "LazyStringUtf8" $ nf U8.toString lbs
49 , bench "LazyStringUtf8" $ nf U8.toString lbs
42 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
50 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
43 ]
51 ]
52+
53+iconv :: ByteString -> IO CInt
54+iconv (PS fp off len) = withForeignPtr fp $ \ptr ->
55+ time_iconv (ptr `plusPtr` off) (fromIntegral len)
56+
57+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