File: sqrt.ml

package info (click to toggle)
planets 0.1.13-19
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 512 kB
  • sloc: ml: 4,541; makefile: 207; ansic: 38
file content (156 lines) | stat: -rwxr-xr-x 3,163 bytes parent folder | download | duplicates (9)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
open Printf


type fref = { mutable v: float }

let iterations = try int_of_string(Sys.argv.(1)) with _ -> 10000
let x = { v = 1.0 }		   
let y = { v = 1.0 }		   


(* first sqrt *)
let timer = MTimer.create ()

let _ =
  MTimer.start timer;
  for i = 0 to iterations - 1 do
    y.v <- y.v +. 1.0;
    x.v <- sqrt(y.v)
  done;
  MTimer.stop timer
let _ = 
  printf "Sqrt: %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)


(* Then exponent *)
let _ =
  MTimer.start timer;
  for i = 0 to iterations - 1 do
    y.v <- y.v +. 1.0;
    x.v <- y.v**(0.51)
  done;
  MTimer.stop timer
let _ = 
  printf "Pow:  %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)


(* Then exponent *)
let _ =
  MTimer.start timer;
  for i = 0 to iterations - 1 do
    y.v <- y.v +. 1.0;
    x.v <- y.v*.y.v
  done;
  MTimer.stop timer
let _ = 
  printf "Sqr:  %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)


let _ =
  MTimer.start timer;
  for i = 0 to iterations - 1 do
    y.v <- y.v +. 100.0;
    x.v <- min x.v y.v
  done;
  MTimer.stop timer
let _ = 
  printf "Min:  %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)


let _ =
  MTimer.start timer;
  for i = 0 to iterations - 1 do
    y.v <- y.v +. 100.0;
    x.v <- if x.v < y.v then x.v else y.v
  done;
  MTimer.stop timer
let _ = 
  printf "FMin: %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)



let _ =
  let rec loop i partial = match i with
      0 -> ()
    | _ ->  
	if i < partial 
	then loop (i-1) i else loop (i-1) partial
  in
    MTimer.start timer;
    loop iterations (iterations/2);
    MTimer.stop timer
let _ = 
  printf "FMin: %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)



let _ =
  MTimer.start timer;
  for i = 0 to iterations - 1 do
    y.v <- y.v *. 1.000000001;
  done;
  MTimer.stop timer
let _ = 
  printf "Prod: %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)


let _ =
  MTimer.start timer;
  for i = 0 to iterations - 1 do
    y.v <- y.v +. 1.0;
  done;
  MTimer.stop timer
let _ = 
  printf "Plus: %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)


let x = ref 0
let _ =
  MTimer.start timer;
  for i = 0 to iterations - 1 do
    x := !x + i
  done;
  MTimer.stop timer
let _ = 
  printf "IPls: %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)


type interval = { low: float; 
		  high: float;
		}

let intersect a b = { low = if a.low > b.low then a.low else b.low;
		      high = if a.high < b.high then a.high else b.high;
		    }

let isempty i = i.low >= i.high
let empty = { low = 1.; high = 0. }
let int1 = { low = 0.0; high = 4.2;}
let int2 = { low = 0.9; high = 3.4;}


(*
let intersect (alow,ahigh) (blow,bhigh) = (max alow blow),(min ahigh bhigh)
let int1 = (0.0,4.2)
let int2 = (0.9, 3.4)
*)
(* Then exponent *)
let rec loop n partial = match n with
    0 -> ()
  | _ -> loop (n - 1) (intersect int1 partial)
let _ =
  MTimer.start timer;
  loop iterations int2;
  MTimer.stop timer
let _ = 
  printf "Intr: %f us per iteration\n" 
    (MTimer.read_us timer /. float iterations)