on master help sign in
Benchmark the performance of iconv.

On my Mac, it takes 33ms, vs about 20ms for the Haskell code.

commit30a05097dedf05be360b442bc20699afc551d806 authorBryan O'Sullivan <bos@serpentine.com> dateFri 8 Jul 2011, 13:33 (15 years ago) parentb869956
3 files changed+50
tests/benchmarks/cbits/time_iconv.c+35 tests/benchmarks/src/Data/Text/Benchmarks/DecodeUtf8.hs+14 tests/benchmarks/text-benchmarks.cabal+1
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+
13-- | Test decoding of UTF-8
24--
35-- Tested in this benchmark:
@@ -16,6 +18,11 @@ module Data.Text.Benchmarks.DecodeUtf8
1618 ( benchmark
1719 ) where
1820
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)
1926import Criterion (Benchmark, bgroup, bench, nf)
2027import qualified Codec.Binary.UTF8.Generic as U8
2128import qualified Data.ByteString as B
@@ -31,6 +38,7 @@ benchmark fp = do
3138 lbs <- BL.readFile fp
3239 return $ bgroup "DecodeUtf8"
3340 [ bench "Strict" $ nf T.decodeUtf8 bs
41+ , bench "IConv" $ iconv bs
3442 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
3543 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
3644 , bench "Lazy" $ nf TL.decodeUtf8 lbs
@@ -41,3 +49,9 @@ benchmark fp = do
4149 , bench "LazyStringUtf8" $ nf U8.toString lbs
4250 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
4351 ]
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
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