List Info

Thread: "ocaml_beginners"::[] Another OCaml vs C




"ocaml_beginners"::[] Another OCaml vs C
country flaguser name
United States
2007-02-11 10:41:49

Hi !

In order to learn OCaml, I begin to program toy applications.
For example : the knight that runs one times on each square of the
chess board.

Here is the complete program : ( Apologize to exhibit such a naughty
thing : it's my 2nd OCaml program. Good thing to have a beginner list... )
http://fabrice.marchant.free.fr/OCaml/knight/one/

The heart is :

...
let b = new board width height in
let d = new display width height box_n in
let rec solve s =
if b#is_free s then(
b#set s;
if b#full () then
b#print d;
List.iter (fun c -> let target =(s ++ c) in
if b#own target then
solve target)
delta;
b#free s
)

The C program p. 10, Fig 16 of :
http://perso.orange.fr/jean-paul.davalan/graphs/cours/graphes.pdf

It works exactly the same way :

void next(int n, int x, int y) { /* position suivante */

int k, X, Y;

quad[x][y] =n;
if( n<NN ) {
for( k =0; k<8; ++k ) {
X =x + DIF[k][0],
Y =y + DIF[k][1];
if( X>=0 && X<N && Y>=0 && Y<N && quad[X][Y]==0 )
next( n+1, X, Y );
}
} else if( n==NN ) { /* affichage de la solution */
++num;
if( !quiet || num<=1 )
solution();
}
quad[x][y] =0;
}

However, ( after restricting OCaml pgm to (0, 0) start square - to
compare same jobs ), the C program appears to be about 10 times speeder.
I thought C/OCaml speed ratio was smaller, say about 2 or 3. But maybe
something goes wrong with my program.

Moreover, I rewrote the program and use ocamlgraph lib :
http://fabrice.marchant.free.fr/OCaml/knight/two/

- building first the graph of knight possible moves in order to avoid
later tests about to be or not inside the board
- then looking for a hamiltonian path of the graph

Unfortunately, the speed results are even worst...

If you know an example of a speed well written chess knight OCaml
program somewhere, I would be interested.

Regards

Fabrice

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Another OCaml vs C
country flaguser name
United States
2007-02-11 12:06:46

2007/2/11, fabrice.marchant < fabrice.marchant%40orange.fr">fabrice.marchantorange.fr>:
[...]
> let b = new board width height in
> let d = new display width height box_n in
[...]
>
&gt; However, ( after restricting OCaml pgm to (0, 0) start square - to
> compare same jobs ), the C program appears to be about 10 times speeder.
> I thought C/OCaml speed ratio was smaller, say about 2 or 3. But maybe
&gt; something goes wrong with my program.

You fell in the object trap : objects are slower in ocaml than the
rest of the language, and not so useful. rewrite your code in plain
caml (with object), compile it ocamlopt, and you will have more
similar result, with one catch : ocaml array bound are by default
checked at each access, will C array are not, then f you want speed
over safety, you can compile with the -unsafe option of the compiler.

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )