caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Currying vs Speed
@ 2002-11-05  2:49 Magesh Kannan
  2002-11-05 17:08 ` Florian Hars
  0 siblings, 1 reply; 2+ messages in thread
From: Magesh Kannan @ 2002-11-05  2:49 UTC (permalink / raw)
  To: caml-list

Hi All,

Does currying one of the arguments to a function provide any run-time 
performance improvements?

In the following code fragment,

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

Does the invocation (my_func_part 10 20) run any faster than
(my_func_wrapper 5 10 20)?

Thanks,
Magesh

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


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

* Re: [Caml-list] Currying vs Speed
  2002-11-05  2:49 [Caml-list] Currying vs Speed Magesh Kannan
@ 2002-11-05 17:08 ` Florian Hars
  0 siblings, 0 replies; 2+ messages in thread
From: Florian Hars @ 2002-11-05 17:08 UTC (permalink / raw)
  To: Magesh Kannan; +Cc: caml-list

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

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

end of thread, other threads:[~2002-11-05 17:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-05  2:49 [Caml-list] Currying vs Speed Magesh Kannan
2002-11-05 17:08 ` Florian Hars

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