package jupyter

  1. Overview
  2. Docs

Source file bench.ml

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(* ocaml-jupyter --- An OCaml kernel for Jupyter

   Copyright (c) 2017 Akinori ABE

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE. *)

(** Benchmark functions *)

open Format

type 'a t =
  {
    b_rtime : 'a; (** Real time for the process *)
    b_utime : 'a; (** User time for the process *)
    b_stime : 'a; (** System time for the process *)
  }

type stat =
  {
    bs_mean : float; (** Mean of execution time of a run *)
    bs_std : float; (** Standard deviation of execution time of a run *)
  }

let pp pp_a ppf x =
  fprintf ppf
    "Jupyter_notebook.Bench.({@;<0 2>@[b_rtime = %a;@ b_utime = %a;@ b_stime = %a;@ @]})"
    pp_a x.b_rtime pp_a x.b_utime pp_a x.b_stime

let pp_timedelta ppf x =
  if x < 1e-6 then fprintf ppf "%.3f ns" (x *. 1e9)
  else if x < 1e-3 then fprintf ppf "%.3f us" (x *. 1e6)
  else if x < 1.0 then fprintf ppf "%.3f ms" (x *. 1e3)
  else if x < 60.0 then fprintf ppf "%.3f s" x
  else begin
    let x, d = Float.modf (x /. 86400.0) in
    let x, h = Float.modf (x *. 24.0) in
    let s, m = Float.modf (x *. 60.0) in
    if d > 0.0 then fprintf ppf "%.0f days " d ;
    fprintf ppf "%.0f:%02.0f:%02.0f" h m s
  end

let pp_float_t = pp pp_timedelta

let pp_stat ppf x =
  fprintf ppf "%a ± %a" pp_timedelta x.bs_mean pp_timedelta x.bs_std

let time f =
  let rt0 = Unix.gettimeofday () in
  let tt0 = Unix.times () in
  let y = f () in
  let rt1 = Unix.gettimeofday () in
  let tt1 = Unix.times () in
  let res = {
    b_rtime = rt1 -. rt0;
    b_utime = tt1.Unix.tms_utime -. tt0.Unix.tms_utime;
    b_stime = tt1.Unix.tms_stime -. tt0.Unix.tms_stime;
  } in
  (y, res)

let stat ~f xs =
  let n = float_of_int (List.length xs) in
  let mean = List.fold_left (fun acc x -> acc +. f x) 0.0 xs /. n in
  let var =
    List.fold_left
      (fun acc x -> let y = f x -. mean in acc +. y *. y)
      0.0 xs /. n in
  {
    bs_mean = mean;
    bs_std = sqrt var;
  }

let timeit
    ?(runs = 5)
    ?(loops_per_run = 1000000)
    ?(before_run = fun () -> ())
    ?(after_run = fun () -> ())
    f
  =
  let lpr = float_of_int loops_per_run in
  let loop () =
    before_run () ;
    let rt0 = Unix.gettimeofday () in
    let tt0 = Unix.times () in
    for _ = 1 to loops_per_run do
      ignore (f ()) ;
    done ;
    let rt1 = Unix.gettimeofday () in
    let tt1 = Unix.times () in
    after_run () ;
    {
      b_rtime = (rt1 -. rt0) /. lpr;
      b_utime = (tt1.Unix.tms_utime -. tt0.Unix.tms_utime) /. lpr;
      b_stime = (tt1.Unix.tms_stime -. tt0.Unix.tms_stime) /. lpr;
    }
  in
  let ts = List.init runs (fun _ -> loop ()) in
  {
    b_rtime = stat ~f:(fun t -> t.b_rtime) ts;
    b_utime = stat ~f:(fun t -> t.b_utime) ts;
    b_stime = stat ~f:(fun t -> t.b_stime) ts;
  }
OCaml

Innovation. Community. Security.