caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] OCaml speed
@ 2001-10-15 16:39 Rolf Wester
  2001-10-16 21:33 ` Markus Mottl
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Rolf Wester @ 2001-10-15 16:39 UTC (permalink / raw)
  To: caml-list

Hi, 

I used the array access example from http://www.bagley.org/~doug/shootout/
to compare c/c++ speed against ocaml. The sources I used are attached below.
Unfortunately I could not confirm the given cpu times which are 0.11 sec for
gcc and 0.13 for ocamlopt. My results on a Compaq Alpha True64 are 
0.05 for cxx, 0.1 for g++ and 0.29 for ocamlopt. Does anybody have an idea 
what could be the reason for this inconsistency? Did I do anything wrong?

Rolf Wester

ocaml
----------

(*
 * $Id: ary3.ocaml,v 1.2 2001/07/28 21:52:55 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * with help from Markus Mottl
 *)

let run n =  
  let time0 = Unix.times() in
  let lix = n - 1 and x = Array.make n 0 and y = Array.make n 0 in
  for i = 0 to lix do 
	x.(i) <- i + 1 
  done;
  for k = 0 to 999 do 
	for i = lix downto 0 do 
	  y.(i) <- x.(i) + y.(i) 
	done 
  done;
  Printf.printf "%d %d\n" y.(0) y.(lix);
  let time1 = Unix.times() in
  Printf.printf "Zeit = %f\n" (time1.Unix.tms_utime -. time0.Unix.tms_utime);;

run 10000;;

ocamlopt -o aa_ml unix.cmxa aa.ml

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

cpp
----

/* -*- mode: c -*-
 * $Id: ary3.gcc,v 1.1 2001/05/31 02:27:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *
 * this program is modified from:
 *   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
 * Timing Trials, or, the Trials of Timing: Experiments with Scripting
 * and User-Interface Languages</a> by Brian W. Kernighan and
 * Christopher J. Van Wyk.
 *
 * I added free() to deallocate memory.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
clock_t cpu_time0;
clock_t cpu_time1;

int
main(int argc, char *argv[]) 
{
	cpu_time0 = clock();

    int n = 10000;
    int i, k, *x, *y;
        
    x = (int *) calloc(n, sizeof(int));
    y = (int *) calloc(n, sizeof(int));

    for (i = 0; i < n; i++) {
        x[i] = i + 1;
    }
    for (k=0; k<1000; k++) {
        for (i = n-1; i >= 0; i--) {
            y[i] += x[i];
        }
    }

    fprintf(stdout, "%d %d\n", y[0], y[n-1]);

    free(x);
    free(y);


	cpu_time1 = clock();

    fprintf(stdout, "%f \n",  float(cpu_time1 - cpu_time0) / float(CLOCKS_PER_SEC));


    return(0);
}

g++/cxx -O3 -o a_cpp aa.cpp


-------------------------------------
Rolf Wester
wester@ilt.fhg.de
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] OCaml speed
  2001-10-15 16:39 [Caml-list] OCaml speed Rolf Wester
@ 2001-10-16 21:33 ` Markus Mottl
  2001-10-16 21:43 ` Doug Bagley
  2001-10-30 16:24 ` Christophe Raffalli
  2 siblings, 0 replies; 6+ messages in thread
From: Markus Mottl @ 2001-10-16 21:33 UTC (permalink / raw)
  To: Rolf Wester; +Cc: caml-list

Rolf Wester schrieb am Montag, den 15. Oktober 2001:
> ocaml. The sources I used are attached below.  Unfortunately I could
> not confirm the given cpu times which are 0.11 sec for gcc and 0.13
> for ocamlopt. My results on a Compaq Alpha True64 are 0.05 for cxx,
> 0.1 for g++ and 0.29 for ocamlopt. Does anybody have an idea what
> could be the reason for this inconsistency? Did I do anything wrong?

The Alpha/True64 is a completely different beast than an Intel/Linux
platform. The same holds for a comparison of cxx and g++. This may well
explain quite a lot.

> ocamlopt -o aa_ml unix.cmxa aa.ml

Try this instead:

  ocamlopt -unsafe -noassert -o aa_ml unix.cmxa aa.ml

This may squeeze out some cycles...

Regards,
Markus Mottl

-- 
Markus Mottl                                             markus@oefai.at
Austrian Research Institute
for Artificial Intelligence                  http://www.oefai.at/~markus
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] OCaml speed
  2001-10-15 16:39 [Caml-list] OCaml speed Rolf Wester
  2001-10-16 21:33 ` Markus Mottl
@ 2001-10-16 21:43 ` Doug Bagley
  2001-10-30 16:24 ` Christophe Raffalli
  2 siblings, 0 replies; 6+ messages in thread
From: Doug Bagley @ 2001-10-16 21:43 UTC (permalink / raw)
  To: Rolf Wester; +Cc: caml-list

Rolf Wester wrote:
> Hi, 
> 
> I used the array access example from http://www.bagley.org/~doug/shootout/
> to compare c/c++ speed against ocaml. The sources I used are attached below.
> Unfortunately I could not confirm the given cpu times which are 0.11 sec for
> gcc and 0.13 for ocamlopt. My results on a Compaq Alpha True64 are 
> 0.05 for cxx, 0.1 for g++ and 0.29 for ocamlopt. Does anybody have an idea 
> what could be the reason for this inconsistency? Did I do anything wrong?

I don't think you are necessarily doing anything "wrong", but you are
testing in a manner quite differently from me.

Some ideas:
- Our architectures are quite different, I'm using an old PII-450Mhz.
- I time the programs externally, you time them internally.
- I compile the ocaml source with:
  > ocamlopt -noassert -unsafe -ccopt -O3 ary.ml -o ary.run
  You apparently compiled with:  
  > ocamlopt -o aa_ml unix.cmxa aa.ml

The last one is probably what makes the most difference.

Cheers,
Doug
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] OCaml speed
  2001-10-15 16:39 [Caml-list] OCaml speed Rolf Wester
  2001-10-16 21:33 ` Markus Mottl
  2001-10-16 21:43 ` Doug Bagley
@ 2001-10-30 16:24 ` Christophe Raffalli
  2 siblings, 0 replies; 6+ messages in thread
From: Christophe Raffalli @ 2001-10-30 16:24 UTC (permalink / raw)
  To: Rolf Wester; +Cc: caml-list

Rolf Wester a écrit :
> 
> Hi,
> 
> I used the array access example from http://www.bagley.org/~doug/shootout/
> to compare c/c++ speed against ocaml. The sources I used are attached below.
> Unfortunately I could not confirm the given cpu times which are 0.11 sec for
> gcc and 0.13 for ocamlopt. My results on a Compaq Alpha True64 are
> 0.05 for cxx, 0.1 for g++ and 0.29 for ocamlopt. Does anybody have an idea
> what could be the reason for this inconsistency? Did I do anything wrong?
> 

Your code mainly does integer operations and array accesses.

The main difference between C and Ocaml are the reserved bit in integer used by
ocaml for the GC.
Then the generated assembly code are really different and it is possible that
this diffrence cost nothing (almost) on a pentium and a lot on a Alpha True64.

May be Xavier Leroy could look at the assembly code to tell if I am right ?

You could also try with float arrays which should make less difference for your
code ?

-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] OCaml speed
  2001-10-17  4:11 Jeremy Fincher
@ 2001-10-17  8:01 ` Rolf Wester
  0 siblings, 0 replies; 6+ messages in thread
From: Rolf Wester @ 2001-10-17  8:01 UTC (permalink / raw)
  To: caml-list

Thanks to all for your replies,

I tried 

	ocamlopt -noassert -unsafe -ccopt -O3 unix.cmxa ary.ml -o ary

and got 0.21 instead of 0.28 sec. -noaasert and -ccopt -O3 can be omitted
with the same result. So ocaml is about 100% slower compared to g++ 
on a True64 Alpha. I think for many applications this doesn't matter. 

With kind regards

Rolf Wester   
-------------------------------------
Rolf Wester
wester@ilt.fhg.de
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] OCaml speed
@ 2001-10-17  4:11 Jeremy Fincher
  2001-10-17  8:01 ` Rolf Wester
  0 siblings, 1 reply; 6+ messages in thread
From: Jeremy Fincher @ 2001-10-17  4:11 UTC (permalink / raw)
  To: rolf.wester, caml-list

Try compiling with the -unsafe option.  The speed difference is most likely
largely due to the bounds-checking that .() does by default.

Jeremy

>ocamlopt -o aa_ml unix.cmxa aa.ml


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-10-30 19:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-15 16:39 [Caml-list] OCaml speed Rolf Wester
2001-10-16 21:33 ` Markus Mottl
2001-10-16 21:43 ` Doug Bagley
2001-10-30 16:24 ` Christophe Raffalli
2001-10-17  4:11 Jeremy Fincher
2001-10-17  8:01 ` Rolf Wester

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).