Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] c is 4 times faster than ocaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Prevost <j.prevost@g...>
Subject: Re: [Caml-list] c is 4 times faster than ocaml?
On Wed, 4 Aug 2004 15:24:28 +1000 (EST), effbiae@ivorykite.com
<effbiae@ivorykite.com> wrote:
> was the C hard to read or the O'Caml?  Any style tips for my caml?

Mmm.  They were both pretty blinding.  For simple Caml style, read
some code that's around.  There's bad style and good style and
inbetween style, but it all pretty much works.

> if i write a c extension that mmaps and msyncs then will the vector
> element assignment become a call rather than a movb (or movl)?  that is,
> is Bigarray a 'special' c extension that ocaml knows how to optimize and
> access just like C or is it a c extension that i can model my C extension
> code on?

The basic idea is that you would take something that you might
otherwise do as a long sequence of calls and turn it into a single
call.  For example, if you're interested in blitting strings (which
are essentially byte arrays) into a Bigarray containing bytes, you
might write a C function that checks the bounds one time, converts the
O'Caml integers to native C integers one time, and then just does the
fastest memory copy it can.  This will turn into a function call, but
since the main idea is mainly just to amortize the necessary overhead
across a larger amount of data, it should be preferable.

> can i specify that an int32 is held in a register or does the compiler do
> this?

I would expect (and I may be mistaken) that if you have an int32 that
is scoped to just a given function or loop, you can expect it to go
into a register (if there are enough registers to go around.)  Or, for
example, when you have a single expression (Int32.add 5l (Int32.mul x
3l)) it's not going to be allocating a box for all of those constants,
nor for an intermediate result.  When in doubt, try it and take a look
at the assembly file from ocamlopt -S to get a feel for how things
work.  Note that I would generally recommend that you only go to these
lengths when you know it's going to be an issue.  And only after
you've actual evidence that the system is indeed not fast enough. 
Your chosen testcase has more necessary overhead than most, mainly
because it's interacting heavily with a datastructure *meant* to
interoperate with C.  On the whole, ocamlopt produces binaries that
are very fast.  Just remember that it does best when you write things
in the most natural way for this language, and that learning what's
natural in O'Caml will take a little exposure.

John.

-------------------
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