Loading ...
Sorry, an error occurred while loading the content.

11378jocaml

Expand Messages
  • babelfish007
    Sep 1, 2009
    • 0 Attachment
      Hi,

      I hope it's okay to post this in "ocaml beginners," even though it's a JoCaml question. I'm interested in using JoCaml to hopefully speed up some programs by using multiple processors. As a simple test, I wrote a program that computes Fibonacci numbers in each language:

      JOCAML
      --------------
      def fib(n) =
      if n <= 1 then reply 1 to fib
      else reply fib(n-1) + fib(n-2) to fib
      ;;

      let get_fibs ti =
      let answers = ref [] in
      for iteration = 1 to ti do
      answers := fib(iteration)::!answers;
      done;
      !answers;;

      let fibs = ref [] in
      for i = 1 to 200 do
      fibs := get_fibs 30;
      done;;

      OCAML
      --------------

      let rec fib n =
      if n <= 1 then 1
      else fib (n-1) + fib (n-2);;

      let get_fibs ti =
      let answers = ref [] in
      for iteration = 1 to ti do
      answers := fib iteration::!answers;
      done;
      !answers;;

      let fibs = ref [] in
      for i = 1 to 200 do
      fibs := get_fibs 30;
      done;;

      I compiled these to native code as follows:

      ocamlopt ocaml-code.ml was named o_fibo
      jocamlopt ocaml-code.ml was named jo_fibo
      jocamlopt jocaml-code.ml was named jo_fibjo

      I used a Python script to run and time each program and got this output:

      dgclark: [/opt/msw] pylon $ python compare_jo_to_o.py
      ./jo_fibo
      ./o_fibo
      ./jo_fibjo
      Time elapsed for ./jo_fibo is 14.8446891308
      Time elapsed for ./o_fibo is 20.3937339783
      Time elapsed for ./jo_fibjo is 20.2132298946

      A few things puzzle me here:
      1) The jocaml compiler consistently generates a faster executable than the ocaml compiler when given the same ocaml code.
      2) The jocaml compiler consistently generates a slower executable from jocaml code than from ocaml code.
      3) When watching the activity of the two processors on my machine (a dual 2.0 GHz PowerPC G5, running OS X 10.5) I don't see any increased tendency to use both processors while the jo_fibjo program is running.

      My questions are:

      1) Can I really expect JoCaml to run faster than Ocaml on machines with multiple processors?
      2) Is the problem here my code? i.e., have I missed something about how to write truly concurrent code in JoCaml? (I got this fibonacci function from the JoCaml introduction online.)

      Thanks if anyone can help.

      David
    • Show all 4 messages in this topic