Last active
January 3, 2016 19:29
-
-
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)
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
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) | |
] |
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
all: | |
ghc -O2 -fforce-recomp ffi-overhead.hs return.c && ./ffi-overhead |
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
benchmarking safe | |
mean: 89.20918 ns, std dev: 1.544843 ns | |
benchmarking unsafe | |
mean: 7.492741 ns, std dev: 173.1078 ps |
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
void print(int x){ | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment