* tuple vs curried, records vs variants
@ 1999-07-28 18:48 Jens Olsson
1999-08-02 20:17 ` Xavier Leroy
0 siblings, 1 reply; 2+ messages in thread
From: Jens Olsson @ 1999-07-28 18:48 UTC (permalink / raw)
To: caml-list
Hello,
(Yet another 'newbie'-question for anyone who has the patience)
Different sources (such as the documentation) says curried functions
are to prefer rather than functions using tuples, if speed is of any
concern anyway.
Before realizing this and as I'm used to the SML dialect, I
implemented a module for three-dimensional vectors using 'tupled'
functions first. Upon a question regarding speed I also got the
recommendation to use records instead of a variant with a single
constructor.
After making all the functions curried instead, and changing the
implementation to use records instead I measured the performance of
the two implementation and realized to my surprise that the old
version (the implementation using variants and tupled functions) was
faster (than the one using records and curried functions). So my
questions are:
1) Is this really correct? I tried the implementations on two different
architectures and got the same result. One can ask if the test program
wasn't 'good enough'.
2) Is it only a "religious" question to use single-valued variants
instead of records (when defining a datatype) or has speed something
to do with it? If the former case applies, which one should I use
and why? Any other aspects?
3) BTW, what is the "best" way of writing pattern-matching functions,
ie [let f x = match ....] or [let f = function x -> ...]?
(Maybe we're getting 'religious' here as well?)
I guess my code is not of interest so I'll exclude it but if someone
thinks it's relevant I can provide it.
Thanks in advance for any response to my somewhat basic questions...
best regards
Jens
--
--[ Jens Olsson ]-----------------------------------------------------
[Address] [Phone] [WWW]
Djäknegatan 13, 1tr Home: 018 - 24 88 77 jenso@csd.uu.se
754 23 Uppsala Work: 018 - 471 76 85 www.csd.uu.se/~jenso
SWEDEN Cell: CEASED!!! ICQ UID 8955231
---------------------------------[ CS student @ Uppsala University ]--
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: tuple vs curried, records vs variants
1999-07-28 18:48 tuple vs curried, records vs variants Jens Olsson
@ 1999-08-02 20:17 ` Xavier Leroy
0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 1999-08-02 20:17 UTC (permalink / raw)
To: Jens Olsson, caml-list
> Different sources (such as the documentation) says curried functions
> are to prefer rather than functions using tuples, if speed is of any
> concern anyway.
This is definitely true for the bytecode compiler, which can execute
curried function applications with no heap allocation at all in the
common cases, while building a tuple of arguments always entail some
heap allocation.
For the native-code compiler, an optimization for "tupled" function
calls was added some time ago, which removes the heap allocation
of the tuple of arguments in the case of calls to "known" functions.
There are still some cases of unknown function calls where the curried
form is more efficient than the tupled form, but it shouldn't make a
big difference.
Note that this discussion assumes that the tuple of arguments needs
rebuilding at each call. If you often pass the tuple of arguments
unmodified to another function, as in
let f ((x,y,z) as t) = ... g t ... h t ...
the cost of allocating the tuple is amortized on several calls, and
the fact that only one argument needs to be passed to each function
may further decrease the cost.
> Upon a question regarding speed I also got the
> recommendation to use records instead of a variant with a single
> constructor.
The only case where it matters is if all fields of the record have
type "float". In this case, the record will be represented, accessed
and built more efficiently than the one-constructor variant. In all
other situations, the compilers generate pretty much the same code for
both styles.
> After making all the functions curried instead, and changing the
> implementation to use records instead I measured the performance of
> the two implementation and realized to my surprise that the old
> version (the implementation using variants and tupled functions) was
> faster (than the one using records and curried functions).
I would have to see the code to try and understand what's happening.
> 3) BTW, what is the "best" way of writing pattern-matching functions,
> ie [let f x = match ....] or [let f = function x -> ...]?
> (Maybe we're getting 'religious' here as well?)
Much more so here, because the compilers will generate identical code
for the two forms, so it's purely a matter of syntactic taste.
Best regards,
- Xavier Leroy
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1999-08-12 10:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-07-28 18:48 tuple vs curried, records vs variants Jens Olsson
1999-08-02 20:17 ` Xavier Leroy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox