Skip to content

Instantly share code, notes, and snippets.

@eras
Created October 22, 2017 09:43
Show Gist options
  • Save eras/8540363812c95b5f7a6ab1704531146c to your computer and use it in GitHub Desktop.
Save eras/8540363812c95b5f7a6ab1704531146c to your computer and use it in GitHub Desktop.
Simple C call time benchmark
#!/bin/sh
set -e
ocamlfind query bench || opam install bench
ocamlfind ocamlopt -linkpkg -o foo foo-c.c foo.ml -package unix,bench
#include <caml/memory.h>
static int x = 0;
void foo_test(value nothing)
{
CAMLparam0();
++x;
CAMLreturn0;
}
value foo_info(value nothing)
{
CAMLparam0();
CAMLreturn(Val_int(x));
}
external foo : unit -> unit = "foo_test"
external info : unit -> int = "foo_info"
let main () =
Printf.printf "counter before: %d\n%!" (info ());
Bench.bench [("call c function", foo)];
Printf.printf "counter after: %d\n%!" (info ())
let _ = main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment