Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Numeric programming efficiency question
@ 1999-03-22 18:33 James Hague
  1999-03-23 17:12 ` Xavier Leroy
  0 siblings, 1 reply; 3+ messages in thread
From: James Hague @ 1999-03-22 18:33 UTC (permalink / raw)
  To: caml-list

First of all, let me say that I've been having a great time learning
Objective CAML!

I implemented some simple functions that operate on three dimensional
vectors.  After reading the "Numeric Programming in CAML" document, it
seems that, unfortunately, the code resulting from using a more classic
syntax is less efficient than using structures.  That is, this:

let vadd (x0,y0,z0) (x1,y1,z1) = (x0 +. x1, y0 +. y1, z0 +. z1);;

generates poorer code than:

type vector = {x: float; y: float; z: float};;
let vadd a b = {x = a.x +. b.x; y = a.y +. b.y; z = a.z +. b.z};;

When using this function, one implementation has a more concise calling
syntax:

vadd (1.0,2.0,3.0) (10.0,20.0,30.0);;
vadd {x=1.0;y=2.0;z=3.0} {x=10.0;y.0;z=30.0};;

A utility routine makes the second option a little nicer:

let vec (a,b,c) = {x=a; y=b; z=c};;

This lets one write:

vadd vec(1.0,2.0,3.0) vec(10.0,20.0,30.0);;

I'm curious if the "shape changing" vec routine is optimized away in such
an expression.  I would expect it to be, but that's just the wishful
programmer in me.

James Hague




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

end of thread, other threads:[~1999-03-24 14:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-22 18:33 Numeric programming efficiency question James Hague
1999-03-23 17:12 ` Xavier Leroy
1999-03-23 19:32   ` James Hague

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox