On Tuesday 22 April 2008 18:45:28 Richard Warburton wrote:
> Could you please provide the source code for this performance comparison.

Sure. The Java:

public class test
{
    int foo(int n)
    {
      return n - 1;
    }

    void bar()
    {
    }

    void baz()
    {
    }

    void run()
    {
        Exception e = new Exception("");
        try
        {
            for (int i=0; i<3; ++i)
            {
                if (foo(i) == 0) throw e;
            }
        }
        catch (Exception e2)
        {
        }
        bar();
        baz();
    }

    public static void main(String[] args)
    {
        for (int n=0; n<10; ++n)
        {
            long start = System.currentTimeMillis();
            for (int i=0; i<1000000; ++i)
                (new test()).run();
            System.out.println(System.currentTimeMillis() - start);
        }
    }
}

The F# (for both techniques):

#light

let foo n = n-1
let bar() = ()
let baz() = ()

exception StopIteration

let run1() =
  try
    for n=0 to 2 do
      if foo n=0 then raise StopIteration
  with StopIteration ->
    ()
  bar()
  baz()

let run2() =
  let rec run_1 n =
    if foo n=0 then run_2() else
      if n<3 then run_1(n+1) else run_2()
  and run_2() =
    bar()
    baz()
  run_1 0

do
  let t = new System.Diagnostics.Stopwatch()
  t.Start()
  for i=1 to 1000000 do
    run1()
  printf "Exceptions: %dms\n" t.ElapsedMilliseconds
  t.Reset()
  t.Start()
  for i=1 to 1000000 do
    run2()
  printf "Tail calls: %dms\n" t.ElapsedMilliseconds
stdin.ReadLine()

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM 
Languages" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to