Skip to content

Instantly share code, notes, and snippets.

@nh2
Last active January 3, 2016 19:29
Show Gist options
  • Save nh2/8508695 to your computer and use it in GitHub Desktop.
Save nh2/8508695 to your computer and use it in GitHub Desktop.
Haskell: Benchmark safe foreign call overhead (inspired by https://groups.google.com/forum/#!topic/haskell-cafe/umeWETniZDM)
import Criterion.Main
import Foreign.C
foreign import ccall safe "print" printSafe :: CInt -> IO ()
foreign import ccall unsafe "print" printUnsafe :: CInt -> IO ()
main = do
defaultMain
[ bench "safe" $ nfIO (printSafe 10)
, bench "unsafe" $ nfIO (printUnsafe 10)
]
all:
ghc -O2 -fforce-recomp ffi-overhead.hs return.c && ./ffi-overhead
benchmarking safe
mean: 89.20918 ns, std dev: 1.544843 ns
benchmarking unsafe
mean: 7.492741 ns, std dev: 173.1078 ps
void print(int x){
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment