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

"Gabriel Scherer [email protected] [ocaml_beginners]" <[email protected]> Mon, 7 Mar 2016 12:17:48 -0500
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAPFanBFBBKWDK3nJpy-iFdBravnb1AsKRO5Ct21QPRnEiacmNQ@mail.gmail.com>
You should also show us the code that does the actual calls to your timing
functions. It's possible that you may have assumed too much of evaluation
order, for example, and that these calls are made at a time you do not
expect.

On Mon, Mar 7, 2016 at 12:15 PM, Douglas Lewit [email protected]
[ocaml_beginners] <[email protected]> wrote:

>
>
> 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
>
>
>
>
> 
>