File: numbers.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 (51 lines) | stat: -rw-r--r-- 1,488 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
open Printf
open Benchmark

(* Test the speed of addition for native ints (unboxed), and
   Int32/Int64 (which are both boxed).

   The output looks something like numbers.out
*)

let f_int n =
  let rec loop i sum =
    if i < n then loop (i + 1) (sum + 1) else sum in
  loop 0 0

let f_int32 n =
  let rec loop i sum =
    if i < n then loop (i + 1) (Int32.add sum Int32.one) else sum in
  Int32.to_int (loop 0 Int32.zero)

let f_int64 n =
  let rec loop i sum =
    if i < n then loop (i + 1) (Int64.add sum Int64.one) else sum in
  Int64.to_int (loop 0 Int64.zero)

let () =
  (* print out the results of the f_* functions to doublecheck that
     they work as we intend. *)
  printf "f_int   666 = %d\n" (f_int   666);
  printf "f_int32 666 = %d\n" (f_int32 666);
  printf "f_int64 666 = %d\n" (f_int64 666);
  print_newline ();

  (* let's exercise the *1 functions: *)
  let _ = latency1 ~name:"int-1-lat" 1000L f_int 10000 in
  let _ = throughput1 ~name:"int-1-thru" 5 f_int 10000 in
  print_newline ();

  (* now let's exercise the *N functions: *)
  let res = throughputN ~repeat:5 10
              [("int",   f_int,   10000);
               ("int32", f_int32, 10000);
               ("int64", f_int64, 10000); ] in
  print_newline ();
  tabulate res;

  print_newline ();
  let res = latencyN 2000L [("int",   f_int,   10000);
                            ("int32", f_int32, 10000);
                            ("int64", f_int64, 10000); ] in
  print_newline ();
  tabulate res