Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Benchmark stdio functions via the FFI
- Loading branch information
Showing
3 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 -> () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |