Sys.time( ) and Unix.gettimeofday( ) ;;

"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Mon, 7 Mar 2016 11:15:03 -0600
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAM0XMJRg3S2wog=8ypEVv=A3obOpA1dvt_vULc+uZxR+PD70ew@mail.gmail.com>
Hi everyone,

I wrote two different functions to generate a list of partial sums if given
a list of floats.

Here are my functions:

*(* Recursively compute the partial sums of a list of floats. Not
tail-recursive and might be too slow for very long lists. *)*

*let partialSums lst = let rec take n l = match n with *

*|0 -> [ ]*

*|_ -> match l with*

*|[ ] -> [ ]*

*|head :: tail -> head :: ( take ( n - 1 ) tail ) in *

*let rec sum l = match l with *

*|[ ] -> 0.*

*|head :: tail -> head +. sum tail in *

*let rec pSum l k m = if k > m then [ ] else sum ( take k lst ) :: ( pSum l
( k + 1 ) m ) in *

*pSum lst 1 ( List.length lst ) ;;*


*(* Recursively compute the partial sums of a list of floats.
Tail-recursive and therefore more efficient and faster than *

*the preceding function. *)*

*let partialsums lst = let rec psums l accumulator = match l with *

*|[ ] -> [ ]*

*|head :: tail -> ( head +. accumulator ) :: begin psums tail ( head +.
accumulator ) end in *

*psums lst 0. ;;*


*(* Recursively generate a sequence of floats. *)*

*let rec sequence lower upper step = if lower > upper then [ ] else lower
:: ( sequence ( lower +. step ) upper step ) ;;*


The functions work just fine, but I'm having trouble using *Sys.time( )*
and *Unix.gettimeofday( )* in the utop environment.  They are not returning
any error messages, but the values seem very unreliable.  For *partialSums*
for example, the answer should be at least 60 seconds ( if not more ) and
what I'm getting back as an answer is something like 0.000001 seconds,
which makes more sense for *partialsums*, which is by far the more
efficient of the two functions.  Any suggestions?  Thank you!


Best,


Douglas Lewit