Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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
cases.

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

So I looked at the generated assembler:

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

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>