-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsqrt.ml
More file actions
60 lines (54 loc) · 1.63 KB
/
sqrt.ml
File metadata and controls
60 lines (54 loc) · 1.63 KB
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
open Bechamel
let () = Random.self_init ()
let random_max = 32767.
let ( <.> ) f g x = f (g x)
let normal n =
let m = n + (n mod 2) in
let values = Array.create_float m in
for i = 0 to (m / 2) - 1 do
let x = ref 0. and y = ref 0. and rsq = ref 0. in
while
x := (Random.float random_max /. random_max *. 2.0) -. 1.;
y := (Random.float random_max /. random_max *. 2.0) -. 1.;
rsq := (!x *. !x) +. (!y *. !y);
!rsq >= 1. || !rsq = 0.
do
()
done;
let f = sqrt (-2.0 *. log !rsq /. !rsq) in
values.(i * 2) <- !x *. f;
values.((i * 2) + 1) <- !y *. f
done;
Array.map (( *. ) random_max) values
let sqrt n =
let vs = normal n in
Staged.stage @@ fun () ->
for i = 0 to Array.length vs - 1 do
ignore (sqrt vs.(i))
done
let test = Test.make_indexed ~name:"sqrt" ~fmt:"%s %d" ~args:[ 10; 50 ] sqrt
let benchmark () =
let ols =
Analyze.ols ~bootstrap:0 ~r_square:true ~predictors:Measure.[| run |]
in
let instances = Bechamel_perf.Instance.[ cpu_clock ] in
let cfg =
Benchmark.cfg ~limit:2000 ~quota:(Time.second 0.5) ~kde:(Some 1000) ()
in
let raw_results = Benchmark.all cfg instances test in
let results =
List.map (fun instance -> Analyze.all ols instance raw_results) instances
in
let results = Analyze.merge ols instances results in
(results, raw_results)
let () =
let results, _ = benchmark () in
match
Hashtbl.fold
(fun _ v a -> Hashtbl.fold (fun k v a -> (k, v) :: a) v [] :: a)
results []
with
| [ results ] ->
let print (k, ols) = Fmt.pr "%s: %a\n%!" k Analyze.OLS.pp ols in
List.iter print results
| _ -> assert false