I ran a very simple experiment gauging the overhead of OCaml's new
first-class modules.
*direct.ml: *
let f x = x + 1
let _ =
let acc = ref 0 in
for i = 0 to 10000000 do
acc := !acc + (f 1)
done
*first_class_module.ml*
module type S = sig val f : int -> int end
module M = struct let f x = x + 1 end
let package = (module M : S)
let _ =
let acc = ref 0 in
for i = 0 to 10000000 do
let module Local = (val package : S) in
acc := !acc + (Local.f 1)
done
obj.ml
let o = object method f x = x + 1 end
let _ =
let acc = ref 0 in
for i = 0 to 10000000 do
acc := !acc + o#f 1
done
# ocamlopt direct.ml -o direct
# ocamlopt first_class_module.ml -o first_class_module
# ocamlopt obj.ml -o obj
# time ./direct
real 0m0.008s
user 0m0.010s
# time ./first_class_module
real 0m0.012s
user 0m0.020s
# time ./obj
real 0m0.058s
user 0m0.060s
So, the overhead of first class modules is slight, especially when compared
with method invocation.
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs