caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <xavier.leroy@inria.fr>
To: Nuutti Kotivuori <naked+caml@naked.iki.fi>
Cc: Jan Kybic <kybic@fel.cvut.cz>, caml-list@inria.fr
Subject: Re: [Caml-list] Q: Efficient operations on complex numbers
Date: Sat, 24 Jan 2004 10:36:04 +0100	[thread overview]
Message-ID: <20040124103604.A9502@pauillac.inria.fr> (raw)
In-Reply-To: <87broukwzk.fsf@naked.iki.fi>; from naked+caml@naked.iki.fi on Sat, Jan 24, 2004 at 02:31:59AM +0200

> > 3) I could improve the OCaml time to 0.98s by including the code
> > from the Complex module in my source file. (Is there a way of
> > achieving the same result without actually copying it?) On the other
> > hand, it did not matter whether I used tuples or records to
> > represent the complex numbers, whether I defined the 'add' and 'mul'
> > functions locally or at the top level, and whether I asked the
> > compiler to inline or not.
> 
> I am not well versed enough to answer your other questions properly,
> but this made me think there might be something fishy going around.
> 
> OCaml doesn't inline functions from other compilation units right
> now.

Yes, it does, if the .cmx files for the other compilation units are
available.  So, I'm a bit surprised about what Jan Kybic observed.
What I would have expected is that referring to the Complex module or
copying it inside the source file shouldn't change the generated
code, at least if you stay at the default inlining level.

Whether a function from a module M is inlinable or not is determined
when A is compiled, as a function of the inlining level (the -inline option)
given for that compilation.  Hence, copying the source for Complex in
your module and compiling with high -inline could allow more inlining
than what you'd get by referring to the precompiled Complex and
setting a high -inline level.  But you should get the same results by
recompiling Complex with the same -inline level.

Moreover, using tuples instead of records to represent complex numbers
results in additional boxing and should therefore be somewhat slower.

Coming back to Jan's initial question:

> > So far, I have implemented a part of the computational kernel in
> > OCaml that calculates approximations using spherical harmonics. It
> > uses complex values. The Ocaml version is basically is straightforward
> > rewrite of the C++ version and is almost exactly twice as slow as the
> > C++ version. This is intriguing because the parts code that only uses real
> > arithmetic is about as fast as the corresponding C++ version. So why
> > is working with complex numbers so expensive in OCaml and what can be
> > done about it?

You're correct that boxing (of complex numbers) contributes
significantly to the overhead.  For float arithmetic, the OCaml
compiler has special local unboxing elimination rules that enables it
to avoid the overhead of boxing in many situations (but not always).
These unboxing optimizations don't extend to data structures such as
complex numbers.

This said, a slowdown by a factor of 2 due to boxing alone is a bit
surprising -- usually, it's more like 1.5.  There might be other
tricks that the C++ compiler plays and that OCaml doesn't.  I'll look
at your code more carefully when I have some more time.

- Xavier Leroy

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-01-24  9:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-23 10:24 Jan Kybic
2004-01-24  0:31 ` Nuutti Kotivuori
2004-01-24  9:36   ` Xavier Leroy [this message]
2004-01-24 15:40     ` Nuutti Kotivuori
2004-01-26  9:00     ` Jan Kybic

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040124103604.A9502@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=kybic@fel.cvut.cz \
    --cc=naked+caml@naked.iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).