caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] optimizing numerical code
@ 2011-05-18 18:35 Joel Reymont
  2011-05-18 18:50 ` Alain Frisch
  0 siblings, 1 reply; 7+ messages in thread
From: Joel Reymont @ 2011-05-18 18:35 UTC (permalink / raw)
  To: caml-list

Consider the following two functions that I'm trying to optimize.

Why is there an allocation for every iteration of the loop in divergence1 but not in divergence2?

What else can I do to make divergence2 faster, apart from using -unsafe?

	Thanks in advance, Joel

---

let js_divergence1 v1 v2 =
  let acc = ref 0. in
  for i = 0 to (Array.length v1) - 1 do
    let x = v1.(i)
    and y = v2.(i) in
    let m = 0.5 *. (x +. y) in
    let d1 = x *. log (x /. m) 
    and d2 = y *. log (y /. m) in
    acc := !acc +. d1 +. d2
  done;
  (!acc)

let js_divergence2 v1 v2 =
  let sum1 = ref 0.
  and sum2 = ref 0. in
  for i = 0 to (Array.length v1) - 1 do
    let x = v1.(i)
    and y = v2.(i) in
    let m = 2. /. (x +. y) in
    let d1 = x *. log (x *. m) 
    and d2 = y *. log (y *. m) in
    sum1 := !sum1 +. d1;
    sum2 := !sum2 +. d2;
  done;
  !sum1 +. !sum2

ocamlopt -dcmm

(function camlFoo__js_divergence1_1030 (v1/1031: addr v2/1032: addr)
 (let acc/1033 "camlFoo__3"
   (let (i/1034 1 bound/1066 (+ (or (>>u (load (+a v1/1031 -8)) 9) 1) -2))
     (catch
       (if (> i/1034 bound/1066) (exit 17)
         (loop
           (let
             (x/1067
                (seq (checkbound (>>u (load (+a v1/1031 -8)) 9) i/1034)
                  (load float64u (+a (+a v1/1031 (<< i/1034 2)) -4)))
              y/1068
                (seq (checkbound (>>u (load (+a v2/1032 -8)) 9) i/1034)
                  (load float64u (+a (+a v2/1032 (<< i/1034 2)) -4)))
              m/1069 (*f 0.5 (+f x/1067 y/1068))
              d1/1070 (*f x/1067 (extcall "log" (/f x/1067 m/1069) float))
              d2/1071 (*f y/1068 (extcall "log" (/f y/1068 m/1069) float)))
             (assign acc/1033
                       (alloc 1277
                         (+f (+f (load float64u acc/1033) d1/1070) d2/1071))))
           (let i/1065 i/1034 (assign i/1034 (+ i/1034 2))
             (if (== i/1065 bound/1066) (exit 17) []))))
     with(17) []))
   acc/1033))

(function camlFoo__js_divergence2_1040 (v1/1041: addr v2/1042: addr)
 (let (sum1/1055 0. sum2/1056 0.)
   (let (i/1045 1 bound/1058 (+ (or (>>u (load (+a v1/1041 -8)) 9) 1) -2))
     (catch
       (if (> i/1045 bound/1058) (exit 16)
         (loop
           (let
             (x/1059
                (seq (checkbound (>>u (load (+a v1/1041 -8)) 9) i/1045)
                  (load float64u (+a (+a v1/1041 (<< i/1045 2)) -4)))
              y/1060
                (seq (checkbound (>>u (load (+a v2/1042 -8)) 9) i/1045)
                  (load float64u (+a (+a v2/1042 (<< i/1045 2)) -4)))
              m/1061 (/f 2. (+f x/1059 y/1060))
              d1/1062 (*f x/1059 (extcall "log" (*f x/1059 m/1061) float))
              d2/1063 (*f y/1060 (extcall "log" (*f y/1060 m/1061) float)))
             (assign sum1/1055 (+f sum1/1055 d1/1062))
             (assign sum2/1056 (+f sum2/1056 d2/1063)))
           (let i/1057 i/1045 (assign i/1045 (+ i/1045 2))
             (if (== i/1057 bound/1058) (exit 16) []))))
     with(16) []))
   (alloc 1277 (+f sum1/1055 sum2/1056))))

--------------------------------------------------------------------------
- for hire: mac osx device driver ninja, kernel extensions and usb drivers
---------------------+------------+---------------------------------------
http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
---------------------+------------+---------------------------------------





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-06-09  9:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-18 18:35 [Caml-list] optimizing numerical code Joel Reymont
2011-05-18 18:50 ` Alain Frisch
2011-05-19  8:24   ` Alain Frisch
2011-05-19  8:37     ` Joel Reymont
2011-05-19 10:59       ` Alain Frisch
2011-05-19 12:40         ` Gerd Stolpmann
2011-06-09  9:02           ` Alain Frisch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).