Jon Harrop wrote:

> Sure. The Java:
> 
[cut]
>     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);
>         }
>     }
> }

this is not a good benchmark, for two reasons:

1) you are allocating a new object at every loop, but we are 
benchmarking the loops, not the garbage collector :-); you should use 
static methods instead, IMHO;

2) you are allocating a new exception every time; the optimization 
described here [1] works only if the exception is pre-allocated.
[1] http://blogs.sun.com/jrose/entry/longjumps_considered_inexpensive

Here is my modified benchmark which tries to address these issues:

public class loop2
{

     public static final Exception exc = new Exception("");

     static int foo(int n)
     {
       return n - 1;
     }

     static void bar()
     {
     }

     static void baz()
     {
     }

     static void run()
     {
         try
         {
             for (int i=0; i<3; ++i)
             {
                 if (foo(i) == 0) throw exc;
             }
         }
         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)
                 run();
             System.out.println(System.currentTimeMillis() - start);
         }
     }
}

And here are the results:

[EMAIL PROTECTED] tmp $ java loop1
1923
2032
2032
2052
2031
2078
2058
2035
2067
2063
[EMAIL PROTECTED] tmp $ java loop2
9
3
1
1
1
1
1
1
1
1

Trying to interpret the number, I think that after the first iteration 
hotspot decided to JIT compile the loop, and since it can inline the 
exception it ends up with a completely empty loop which is thrown away.

ciao,
Anto

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