Paste: silly benchmark in ocaml
Author: | abez |
Mode: | ml |
Date: | Fri, 28 Aug 2009 23:05:01 |
Plain Text |
type point = Point of float * float * float
let makePoint n =
let fn = float_of_int n in
let x = sin fn in
let y = 3.0 *. cos fn in
let z =
let s = sin fn in
s *. s /. 2.0
in
Point(x,y,z)
;;
let toString (Point(x,y,z)) =
(string_of_float x) ^ "," ^
(string_of_float y) ^ "," ^
(string_of_float z)
;;
let makePoints n = Array.init n makePoint ;;
let pointNorm (Point(x,y,z)) = x *. x +. y *. y +. z *. z ;;
let normalizePoint (Point(x,y,z) as pt) =
let norm = pointNorm pt in
Point( x /. norm, y /. norm, z /. norm)
;;
let normalizePoints arr = Array.map normalizePoint arr ;;
let maxPoint (Point(x,y,z) as pt1) (Point(x2,y2,z2) as pt2) =
Point((max x x2), (max y y2), (max z z2))
;;
let maxPoints arr =
Array.fold_left (fun max curr -> maxPoint max curr) arr.(0) arr
;;
let benchmark n =
print_endline (toString (maxPoints (normalizePoints (makePoints n))))
;;
let runBenchmark () =
print_string "Run #";
print_endline (string_of_int n);
let t1 = Sys.time () in
benchmark 5000000;
let t2 = Sys.time () in
print_endline ("Time: " ^ (string_of_float (t2 -. t1)));
;;
runBenchmark();;
New Annotation