English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
problem calling C-function from Ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-11-01 (02:06)
From: Dan Koppel <superluminal1905@y...>
Subject: problem calling C-function from Ocaml
Hello all,
  I'm trying to use a C-function called from inside an Ocaml function.  I believe I'm using the correct syntax, but I'm getting some strange results.  Therefore something I'm doing must be wrong.
Here's my C-function:
float myCTime(int dummy) {
        float factor = 1.0;
        return (factor*((float) clock()));
Here's how it's being called from Ocaml:
let dummy = 0 in
print_float (myCamlTime dummy); (* first call *)
print_newline ();
some code here which always takes exactly 30 seconds
print_float (myCamlTime dummy); (* second call *)
print_newline ();
The output I get is:
So, it seems that the time returned is in units of "centiseconds".  However, when I change the variable "factor" in the C-function to 2.0, the output is exactly the same!  I know this function IS being called because when I replace the last line with "return 0.0", the output changes (both calls return the same number).
Here is the C "stub function" that I use:
value stub_myCTime(
        value _v_dummy)
  int dummy; /*in*/
  float _res;
  value _vres;
  dummy = Int_val(_v_dummy);
  _res = myCTime(dummy);
  _vres = copy_double(_res);
  return _vres;
value stub_myCTime_bytecode(value * argv, int argn)
  return stub_myCTime(argv[0]);
and here is the .ml/.mli file that I use:
external myCamlTime : int -> float
        = "stub_myCTime_bytecode" "stub_myCTime"
But something even more perplexing occurs when I insert a printf statement in the C-function.  The output of both calls is now "0.".  I am totally baffled.
Could a kind soul please tell me what, if anything, I may be doing wrong?  All I seek, actually, is an Ocaml function that returns CPU time used by this process with a very good accuracy and very small granularity.  The Unix function "gettimeofday" is good but it returns actual (clock) time, rather than CPU time.
Any help is greatly appreciated!
Sincerely, Dan
PS. Changing float => double in the C-function does not help.

 Yahoo! FareChase - Search multiple travel sites in one click.