From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: jserot@epcc.ed.ac.uk (Jocelyn Serot)
Cc: caml-list@margaux.inria.fr
Subject: Re: Q: float arrays
Date: Wed, 30 Oct 1996 14:40:38 +0100 (MET) [thread overview]
Message-ID: <199610301340.OAA26778@pauillac.inria.fr> (raw)
In-Reply-To: <20086.199610291112@tufa.epcc.ed.ac.uk> from "Jocelyn Serot" at Oct 29, 96 11:12:54 am
> Trying to quantify to cost of handling arrays of tuples instead of
> multiple "parallel" arrays, i recently came to some results, which at
> first sight, seems a little surprising.
> What i cannot understand is that map2 seems to be slower than
> map_pairs (in the range 1->2 in native compiled mode). The few i
> know about the Caml compiler is that it doesnt boxe floats in
> arrays.
The bytecode compiler keeps floats boxed in arrays, so there is no
essential difference between an array of pairs of floats and two float
arrays. (The array of pairs entails one extra indirection and is less
space efficient, but has better locality.)
The native-code compiler unboxes floats in arrays. But unboxing is a
double-edged sword. When the float array is accessed as a float
(e.g. to perform arithmetic on it), unboxed arrays are a big win.
When the float array is accessed as a Caml value (e.g. to pass to
another function, or from a piece of polymorphic code), the unboxed
float needs to be boxed first to convert it to a regular Caml value,
and this is expensive.
Your map2 function actually performs two boxing operations at each
iteration, because the two float elements are passed to a function. It
also has to test dynamically the kind of the array (float
vs. non-float), because it's a polymorphic function. This explains
why it's slower than map on an array of pairs (which involves no
boxing, just 3 indirections per iteration)
If you inline the "*." operation in map2, all boxing will be
eliminated (and all dynamic tests as well, since the function is now
monomorphic), and you'll get much better performance than what can be
obtained with an array of pairs:
let product f a a' =
let l = length a in
if ( length a' != l ) then invalid_arg "product" else
if l = 0 then [||] else begin
let r = create l (f(unsafe_get a 0)(unsafe_get a' 0)) in
for i = 1 to l - 1 do
unsafe_set r i (unsafe_get a i *. unsafe_get a' i)
done;
r
end
Polymorphism and higher-order functions don't mix well with high
performance. If you need Fortran-like performance, there are cases
where you must write Fortran-style code.
- Xavier Leroy
next prev parent reply other threads:[~1996-10-30 13:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-10-29 11:12 Jocelyn Serot
1996-10-30 13:40 ` Xavier Leroy [this message]
1996-10-30 14:08 ` Jocelyn Serot
1996-10-30 14:52 ` Thorsten Ohl
1996-10-31 15:26 ` Xavier Leroy
1996-11-05 16:56 ` Controlling inlining [was Re: Q: float arrays] rowan+
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=199610301340.OAA26778@pauillac.inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@margaux.inria.fr \
--cc=jserot@epcc.ed.ac.uk \
/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