Measuring overhead of safe/unsafe function calls

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Haskell code:

import Control.Monad (forM_)
import Foreign.C.Types (CInt)


foreign import ccall safe "print"
  printsafe :: CInt -> IO ()
foreign import ccall unsafe "print"
  printunsafe :: CInt -> IO ()

main = do
  let  l = 5000000
  forM_ [1..l] $ \x -> printsafe x
  -- use the code below to measure overhead of unsafe call
  -- forM_ [1..l] $ \x -> printunsafe x

C code:

#include <stdio.h>

void print(int x){
  return;
}