Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Numeric programming efficiency question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: James Hague <jhague@d...>
Subject: Numeric programming efficiency question
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

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