Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] time function
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Sven Luther <luther@d...>
Subject: Re: [Caml-list] time function
On Mon, Dec 23, 2002 at 04:05:45PM +0000, onlyclimb wrote:
> Chris Hecker wrote:
> 
> >
> >>How can i get a resolution of milli-seconds
> >
> >
> >Unix.gettimeofday, I believe.  This is in the documentation.
> >
> >
> >
> # open Unix;;
> # time();;
> - : float = 1040659293.
> # gettimeofday();;
> - : float = 1040659299.94
> it seems that  gettimeofday() is for centi-second,  :-)

This code :

let string_of_time t =
  let d = truncate (t /. 86400.)
  in let rd = t -. (float_of_int (d*86400))
  in let h = truncate (rd /. 3600.)
  in let rh = rd -. (float_of_int (h*3600))
  in let m = truncate (rh /. 60.)
  in let rm = rh -. (float_of_int (m*60))
  in let s = truncate rm
  in let rs = rm -. (float_of_int s)
  in let ms = truncate (rs *. 1000.)
  in let rms = rs -. ((float_of_int ms)/.1000.)
  in let mms = truncate (rms *. 1000000.)
  in Printf.sprintf "%d days, %d hours, %d minutes, %d seconds, %d milliseconds, %d microseconds" d h m s ms mms

and later :

let xxx,yyy,aaa = ref 0.0, ref 0.0, ref l in
xxx := Unix.gettimeofday ();
aaa := (* Some calculation *);
yyy := Unix.gettimeofday ();
let time_used = (!yyy -. !xxx) in
Printf.printf "Time used : %f seconds \n" time_used;
Printf.printf "Time used : %s\n" (string_of_time time_used);

Yields :

Time used : 0.035280 seconds 
Time used : 0 days, 0 hours, 0 minutes, 0 seconds, 35 milliseconds, 279 microseconds

So i guess it works with milliseconds, but i may be wrong.

It may just be a coincidence that your call to gettimeofday fall on a
full centi-second, and thus the last decimal was not shown.

Friendly,

Sven Luthr




> 
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: 
> http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: 
> http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners