Skip to content

Commit

Permalink
Benchmark stdio functions via the FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed Jun 27, 2011
1 parent 72a3881 commit 8bf044e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
26 changes: 20 additions & 6 deletions benchmarks/Benchmarks.hs
@@ -1,16 +1,30 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings #-}

import Criterion.Main
import Data.Double.Conversion
import Foreign.C.Types (CInt, CDouble)
import qualified Data.Text as T

showText :: Double -> T.Text
showText d = T.pack (show d)

main = defaultMain [
bench "show" $ whnf showText pi
, bench "toShortest" $ whnf toShortest pi
, bench "toExponential" $ whnf (toExponential 3) pi
, bench "toPrecision" $ whnf (toExponential 8) pi
, bench "toFixed" $ whnf (toFixed 8) pi
bgroup "haskell" [
bench "show" $ whnf showText pi
, bench "toShortest" $ whnf toShortest pi
, bench "toExponential" $ whnf (toExponential 3) pi
, bench "toPrecision" $ whnf (toExponential 8) pi
, bench "toFixed" $ whnf (toFixed 8) pi
]
, bgroup "sprintf" [
bench "exponential" $ whnf (sprintf_exponential 3) pi
, bench "fixed" $ whnf (sprintf_fixed 8) pi
, bench "generic" $ whnf (sprintf_generic 6) pi
, bench "generic_default" $ whnf (sprintf_generic_default 6) pi
]
]

foreign import ccall safe sprintf_exponential :: CInt -> CDouble -> ()
foreign import ccall safe sprintf_fixed :: CInt -> CDouble -> ()
foreign import ccall safe sprintf_generic :: CInt -> CDouble -> ()
foreign import ccall safe sprintf_generic_default :: CInt -> CDouble -> ()
2 changes: 2 additions & 0 deletions benchmarks/double-conversion-benchmarks.cabal
Expand Up @@ -6,6 +6,8 @@ build-type: Simple
executable bm
main-is: Benchmarks.hs

c-sources: sprintf.c

build-depends:
base,
criterion >= 0.5.0.10,
Expand Down
25 changes: 25 additions & 0 deletions benchmarks/sprintf.c
@@ -0,0 +1,25 @@
#include <stdio.h>

void sprintf_exponential(int d, double x)
{
char buf[64];
snprintf(buf, 64, "%*e", d, x);
}

void sprintf_fixed(int d, double x)
{
char buf[64];
snprintf(buf, 64, "%*f", d, x);
}

void sprintf_generic(int d, double x)
{
char buf[64];
snprintf(buf, 64, "%*g", d, x);
}

void sprintf_generic_default(double x)
{
char buf[64];
snprintf(buf, 64, "%g", x);
}

0 comments on commit 8bf044e

Please sign in to comment.