caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Florian Hars <hars@bik-gmbh.de>
To: Magesh Kannan <magesh@ittc.ku.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Currying vs Speed
Date: Tue, 05 Nov 2002 18:08:52 +0100	[thread overview]
Message-ID: <3DC7FB24.9030209@bik-gmbh.de> (raw)
In-Reply-To: <Pine.LNX.4.21.0211042019440.26138-100000@george.ittc.ku.edu>

[-- Attachment #1: Type: text/plain, Size: 838 bytes --]

Magesh Kannan wrote:
> Does the invocation (my_func_part 10 20) run any faster than
> (my_func_wrapper 5 10 20)?

It is comparable if you compile to bytecode, and much worse if you compile to 
native code. In that case, you may loose more than a factor of ten:

$ ./bench
my_func:         16.4 sec
my_func_wrapper: 20.2 sec
my_func_part:    17.1 sec
$ ocamlopt -inline 0 -o bench unix.cmxa bench.ml
$ ./bench
my_func:         0.6 sec
my_func_wrapper: 0.8 sec
my_func_part:    2.2 sec
$ ocamlopt -inline 10 -o bench unix.cmxa bench.ml
$ ./bench
my_func:         0.2 sec
my_func_wrapper: 0.2 sec
my_func_part:    2.3 sec

A full application of a function is optimized far better than a partial one.
Especially in the -inline 10 case, the function call is completely optimized 
away for the two fully applied versions.

Yours, Florian Hars.

[-- Attachment #2: bench.ml --]
[-- Type: text/plain, Size: 909 bytes --]

let my_func arg1 arg2 arg3 =
  let res = arg1 + arg2 + arg3 in
    res

let my_func_wrapper arg1 arg2 arg3 =
  my_func arg1 arg2 arg3

let my_func_part = my_func 5


let _ =
  let t0 = Unix.gettimeofday () in
  let five = 5 in
  let r = ref 0 in
  for i = 0 to 10000 do
    for j = 0 to 10000 do
      r := !r + my_func five i j 
    done
  done;
  Printf.printf "my_func:         %.1f sec\n" (Unix.gettimeofday () -. t0);
  let t0 = Unix.gettimeofday () in
  let five = 5 in
  let r = ref 0 in
  for i = 0 to 10000 do
    for j = 0 to 10000 do
      r := !r + my_func_wrapper five i j 
    done
  done;
  Printf.printf "my_func_wrapper: %.1f sec\n" (Unix.gettimeofday () -. t0);
  let t0 = Unix.gettimeofday () in
  let r = ref 0 in
  for i = 0 to 10000 do
    for j = 0 to 10000 do
      r := !r + my_func_part i j
    done
  done;
  Printf.printf "my_func_part:    %.1f sec\n" (Unix.gettimeofday () -. t0)

      reply	other threads:[~2002-11-05 17:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-05  2:49 Magesh Kannan
2002-11-05 17:08 ` Florian Hars [this message]

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=3DC7FB24.9030209@bik-gmbh.de \
    --to=hars@bik-gmbh.de \
    --cc=caml-list@inria.fr \
    --cc=magesh@ittc.ku.edu \
    /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;
as well as URLs for NNTP newsgroup(s).