English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Multiplication of matrix in C and OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-02-08 (02:14)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Multiplication of matrix in C and OCaml
From: Frédéric Gava <gava@univ-paris12.fr>

> I compared multiplication of matrix in C and OCaml and I was a little 
> surprise to see that the following C code (using -O2) is 8 time faster 
> than the OCaml one (even with -unsafe).
> Anybody have an idea to optimize my OCaml code or know why is there this 
> "big" difference ?
> ps: the difference is the same even when I use a non-polymorphic 
> multiplication

This is not what I see:
Multiplying 1000 times two 50x50 matrices on a Pentium M 1.8G, using
your code.

ocamlopt -unsafe polymorphic: 5.4s
ocamlopt -unsafe -inline 100 polymorphic: 5.0s
ocamlopt -unsafe monomorphic: 4.4s
gcc polymorphic: 6.5s
gcc monomorphic: 5.8s
gcc -O3 monomorphic: 4.7s

So, actually it seems that ocamlopt is even faster than gcc in many

Now, I also found a strange result:
gcc -O3 monomorphic: 1.4s

So I looked at the generated assembler:

.globl add
	.type	add, @function
	pushl	%ebp
	movl	%esp, %ebp
	subl	$56, %esp
	leal	-56(%ebp), %eax
	.size	add, .-add
	.p2align 2,,3
.globl mult
	.type	mult, @function
	pushl	%ebp
	movl	%esp, %ebp
	subl	$56, %esp
	leal	-56(%ebp), %eax

It seems that returning a local value is not well-defined, and gcc
just decides that this value being unused inside the function, there
is no need to do any computation!

Jacques Garrigue      Nagoya University     garrigue at math.nagoya-u.ac.jp
		   <A HREF=http://www.math.nagoya-u.ac.jp/~garrigue/>JG</A>