File: loops.ml

package info (click to toggle)
ocaml-benchmark 0.9-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 236 kB
  • ctags: 222
  • sloc: ml: 689; makefile: 151; sh: 57; perl: 12
file content (34 lines) | stat: -rw-r--r-- 755 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
open Benchmark

(* Test for the speed of recusion w.r.t. imperative loops to access
   arrays fo floats. *)

let rec_loop (a : float array) =
  let rec loop i =
    if i < Array.length a then begin
      a.(i) <- a.(i) +. 1.;
      loop (i + 1)
    end in
  loop 0

let rec_loop2 (a : float array) =
  let len = Array.length a in
  let rec loop i =
    if i < len then begin
      a.(i) <- a.(i) +. 1.;
      loop (i + 1)
    end in
  loop 0

let for_loop (a : float array) =
  for i = 0 to Array.length a - 1 do
    a.(i) <- a.(i) +. 1.
  done

let () =
  let a = Array.make 100 1. in
  let res = throughputN ~repeat:5 1
              [("rec", rec_loop, a);
               ("rec2", rec_loop2, a);
               ("for", for_loop, a); ] in
  tabulate res