caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dan Koppel <superluminal1905@yahoo.com>
To: caml-list@yquem.inria.fr
Subject: problem calling C-function from Ocaml
Date: Mon, 31 Oct 2005 18:06:54 -0800 (PST)	[thread overview]
Message-ID: <20051101020655.49021.qmail@web32209.mail.mud.yahoo.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2091 bytes --]

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:
0.
3000.
 
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.  

[-- Attachment #2: Type: text/html, Size: 3000 bytes --]

             reply	other threads:[~2005-11-01  2:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-01  2:06 Dan Koppel [this message]
2005-11-08 11:54 ` [Caml-list] " Matthieu Dubuget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051101020655.49021.qmail@web32209.mail.mud.yahoo.com \
    --to=superluminal1905@yahoo.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).