caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* min function, why is it so slow?
@ 2010-04-29 18:34 Eray Ozkural
  2010-04-29 18:47 ` [Caml-list] " Markus Mottl
  0 siblings, 1 reply; 3+ messages in thread
From: Eray Ozkural @ 2010-04-29 18:34 UTC (permalink / raw)
  To: caml-list

Hello there,

Although I turn on inlining in ocamlopt (-inline 10), I think that the
min function is not quite inlined. Indeed, it's faster if I just
inline it myself (if a<b a then a else b). It's almost twice as fast
this way. Which makes me thinking. I suppose a procedure call cost is
incurred. This doesn't change when I define min for two parameters
myself in another module. What do you think I am doing wrong?


In  the following, the first loop is (more than) twice as slow, where
min is defined as

let min a b = if a <= b then a else b


                      for i=1 to  10000000 do
                        x1 := !x1 + (Util.min a1.(i-1) a1.(i));
                      done

                      for i=1 to  10000000 do
                        x1 := !x1 + (if a1.(i-1) <= a1.(i) then a1.(i-1)
                                      else a1.(i))

Time elapsed: 0.189798 (first loop)
Time elapsed: 0.079624 (second loop)

Can someone please explain to me what's going on here? Perhaps there
is inlining, but after inlining, some optimization passes aren't
performed?

Best,

-- 
Eray Ozkural, PhD candidate.  Comp. Sci. Dept., Bilkent University, Ankara
http://groups.yahoo.com/group/ai-philosophy
http://myspace.com/arizanesil http://myspace.com/malfunct


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

* Re: [Caml-list] min function, why is it so slow?
  2010-04-29 18:34 min function, why is it so slow? Eray Ozkural
@ 2010-04-29 18:47 ` Markus Mottl
  2010-04-29 19:12   ` Eray Ozkural
  0 siblings, 1 reply; 3+ messages in thread
From: Markus Mottl @ 2010-04-29 18:47 UTC (permalink / raw)
  To: Eray Ozkural; +Cc: caml-list

On Thu, Apr 29, 2010 at 14:34, Eray Ozkural <examachine@gmail.com> wrote:
> Although I turn on inlining in ocamlopt (-inline 10), I think that the
> min function is not quite inlined. Indeed, it's faster if I just
> inline it myself (if a<b a then a else b). It's almost twice as fast
> this way. Which makes me thinking. I suppose a procedure call cost is
> incurred. This doesn't change when I define min for two parameters
> myself in another module. What do you think I am doing wrong?

This is probably a consequence of too much polymorphism.  Your min
function is fully polymorphic, which may prevent the OCaml compiler
from type specialization, i.e. a generic (= slow) comparison function
will be called.  E.g. if your array contains integers or floats, this
is likely to make a significant difference.  Try constraining the type
of your min function to the one of the array elements.  This will
probably make the problem go away.

Regards,
Markus

-- 
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com


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

* Re: [Caml-list] min function, why is it so slow?
  2010-04-29 18:47 ` [Caml-list] " Markus Mottl
@ 2010-04-29 19:12   ` Eray Ozkural
  0 siblings, 0 replies; 3+ messages in thread
From: Eray Ozkural @ 2010-04-29 19:12 UTC (permalink / raw)
  To: Markus Mottl; +Cc: caml-list

On Thu, Apr 29, 2010 at 9:47 PM, Markus Mottl <markus.mottl@gmail.com> wrote:
> On Thu, Apr 29, 2010 at 14:34, Eray Ozkural <examachine@gmail.com> wrote:
>> Although I turn on inlining in ocamlopt (-inline 10), I think that the
>> min function is not quite inlined. Indeed, it's faster if I just
>> inline it myself (if a<b a then a else b). It's almost twice as fast
>> this way. Which makes me thinking. I suppose a procedure call cost is
>> incurred. This doesn't change when I define min for two parameters
>> myself in another module. What do you think I am doing wrong?
>
> This is probably a consequence of too much polymorphism.  Your min
> function is fully polymorphic, which may prevent the OCaml compiler
> from type specialization, i.e. a generic (= slow) comparison function
> will be called.  E.g. if your array contains integers or floats, this
> is likely to make a significant difference.  Try constraining the type
> of your min function to the one of the array elements.  This will
> probably make the problem go away.

Oh, that's a good suggestion, thank you. Let me try it out.

Best,

-- 
Eray Ozkural, PhD candidate.  Comp. Sci. Dept., Bilkent University, Ankara
http://groups.yahoo.com/group/ai-philosophy
http://myspace.com/arizanesil http://myspace.com/malfunct


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

end of thread, other threads:[~2010-04-29 19:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-29 18:34 min function, why is it so slow? Eray Ozkural
2010-04-29 18:47 ` [Caml-list] " Markus Mottl
2010-04-29 19:12   ` Eray Ozkural

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).