Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Basile Starynkevitch [local]" <basile.starynkevitch@inria.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Does Caml have slow arithmetics ?
Date: Wed, 7 Jul 2004 11:13:08 +0200	[thread overview]
Message-ID: <20040707091308.GA26172@bourg.inria.fr> (raw)
In-Reply-To: <Pine.A41.4.44.0407070835130.958588-100000@ibm1>

On Wed, Jul 07, 2004 at 08:45:51AM +0200, Diego Olivier Fernandez Pons wrote:

> Does the Caml integer/pointer bit trick make arithmetics slower (with
> respect to 'pure' 32 bit arithmetics) because of some mask/shift
> intermediate operations or is it just the same ?

In principle yes, because int values are represented as tagged
(31bits) ints (with the LSB set to 1). So conversion is a shift
followed by an addition (or viceversa).

However, ocamlopt does do many optimisations (eg avoiding tagging &
untagging the same value, etc...). Also, on modern processors, simple
ALU operations are extremly fast, faster than most accesses to memory.


> Actually I haven't seen any significative performance difference with
> respect to a similar C++ code I have done, what I wasn't expecting.
> That is why I am asking.

In practice, real code do memory allocation, pattern matching,
etc.. On this, Ocaml excels (and perform probably better than C or
C++); more generally, Ocaml is good for real non-trivial programs.

But for toy programs, arithmetic is a bit faster with C than with
Ocamlopt. On my slow PentiumIII 1GHz, I've tested (the running time is
in comments at end of each example, the Ocaml compiler[s] are the
latest CVS, ie almost future 3.08)

################################################################
(* file eml.ml *)
let slowmul x y = 
  let rec muloop x y p =
    if (y>0) then muloop x (y-1) (p+x) else p
  in 
  if y<0 then muloop (-x) (-y) 0
  else muloop x y 0
;;

let main () = 
  let s = ref 0 in
  for i = 0 to 2000 do
    if (i mod 64 = 0) then Printf.printf "i=%d\n%!" i;
    for j = 0 to 3000 do 
      s := !s  + slowmul i j
    done
  done;
  Printf.printf "s=%d\n%!" !s
in main ();;
(* time ./eml (compiled with ocamlopt -inline 9) is 25.360 total *)
################################################################
// file em.c
#include <stdio.h>
int slowmul(int x, int y) {
  int p=0;
  if (y<0) { x= -x; y= -y; };
  while (y>0) {p+=x; y--;};
  return p;
}

int main() {
  int s=0, i, j;
  for (i=0; i<=2000; i++) {
    if (i%64 == 0) printf("i=%d\n", i);
    for (j=0; j<=3000; j++) 
      s += slowmul(i,j);
  };
  printf("s=%d\n", s);
  return 0;
}
// time ./em (compiled with gcc-3.3 -O3) is 19.095 sec
################################################################

As you can see on this simple test, Ocamlopt produce a program 1.32
times slower than gcc, and if you have a glance at the generated
assembly, you see some tagging stuff appearing.

For completeness, the above eml.ml program bytecompiled by ocamlc to
eml.byte is run in 381.5 seconds -more than 6 minutes- by my
ocamljitrun (see http://cristal.inria.fr/~starynke/ocamljit.html) and
in 1780 seconds (yes, more than 29 minutes, almost half an hour!) by
the standard ocamlrun interpreter.

Regards.
-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359
http://cristal.inria.fr/~starynke --- all opinions are only mine 

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


  parent reply	other threads:[~2004-07-07  9:13 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-07  6:45 Diego Olivier Fernandez Pons
2004-07-07  8:14 ` Richard Jones
2004-07-07  9:13 ` Basile Starynkevitch [local] [this message]
2004-07-07 12:32   ` Diego Olivier Fernandez Pons
2004-07-07 13:00     ` Basile Starynkevitch [local]
2004-07-07 15:09       ` Olivier Pérès
2004-07-07 17:06         ` David Brown
2004-07-07 17:43           ` Olivier Pérès
2004-07-08  3:40             ` David Brown
2004-07-08 11:06               ` Olivier Pérès
2004-07-07 13:06     ` Richard Jones
2004-07-07 13:22     ` David Haguenauer
2004-07-07 15:48     ` Brian Hurt
2004-07-07 13:57   ` Evgeny Chukreev
2004-07-07 14:58     ` Xavier Leroy
2004-07-07 15:48       ` Evgeny Chukreev
2004-07-07 19:16       ` skaller
2004-07-08  3:44         ` David Brown
2004-07-08  5:50           ` skaller
2004-07-08  9:51           ` Andreas Rossberg
2004-07-08 12:03             ` Marcin 'Qrczak' Kowalczyk
2004-07-08 14:04             ` David Brown
2004-07-08 14:36               ` Luc Maranget
2004-07-08 15:11                 ` Alex Baretta
2004-07-08 15:49                   ` Luc Maranget
2004-07-09 14:06                     ` Alex Baretta
2004-07-09 16:20                       ` Markus Mottl
     [not found]                     ` <Luc.Maranget@inria.fr>
2004-07-09 17:54                       ` Norman Ramsey
2004-07-12  8:08                         ` Luc Maranget
2004-07-08 15:51                   ` Markus Mottl
2004-07-08 18:27                     ` skaller
2004-07-08 21:14                     ` [Caml-list] tail recursion and register poor Intel architecture Brandon J. Van Every
2004-07-08 21:35                       ` Brian Hurt
2004-07-08 23:01                         ` Brandon J. Van Every
2004-07-09  4:36                           ` Brian Hurt
2004-07-09  6:53                           ` Florian Hars
2004-07-09 14:44                             ` Brandon J. Van Every
2004-07-09  6:55                           ` skaller
2004-07-09 14:45                             ` Brandon J. Van Every
2004-07-09 16:09                               ` skaller
2004-07-10  9:19                                 ` [Caml-list] embedded OCaml Brandon J. Van Every
2004-07-11 23:11                                   ` Alex Baretta
2004-07-12  7:39                                     ` Brandon J. Van Every
2004-07-12 14:04                                       ` Alex Baretta
2004-07-12 18:48                                         ` Brandon J. Van Every
2004-07-12 23:22                                           ` Richard Jones
2004-07-13  6:39                                             ` Alex Baretta
2004-07-13  8:47                                               ` Brandon J. Van Every
2004-07-13  8:58                                                 ` Benjamin Geer
2004-07-13  9:47                                                   ` Brandon J. Van Every
2004-07-13  9:18                                                 ` Damien Doligez
2004-07-13  9:56                                                   ` [Caml-list] OCaml as business model Brandon J. Van Every
2004-07-13  9:59                                                     ` Richard Jones
2004-07-13 10:50                                                       ` Brandon J. Van Every
2004-07-13 11:20                                                     ` Damien Doligez
2004-07-13 12:01                                                       ` Brandon J. Van Every
2004-07-14  8:05                                                 ` [Caml-list] embedded OCaml I R T
2004-07-12 10:25                                     ` Christophe TROESTLER
2004-07-13  7:06                                       ` Alex Baretta
2004-07-08 17:12                 ` Tail calls (Was Re: [Caml-list] Does Caml have slow arithmetics ?) brogoff
2004-07-08 17:23                   ` Richard Jones
2004-07-12 17:07                   ` Xavier Leroy
2004-07-12 21:13                     ` skaller
2004-07-13  7:20                       ` Xavier Leroy
2004-07-08 15:00             ` [Caml-list] Does Caml have slow arithmetics ? Brian Hurt
2004-07-08 13:30         ` Alex Baretta
2004-07-07 21:26       ` Jon Harrop
2004-07-08  5:05         ` Jacques GARRIGUE

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040707091308.GA26172@bourg.inria.fr \
    --to=basile.starynkevitch@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox