Nathan Meyers wrote:

> Nick Lawson wrote:
> >
> > I'm pretty sure try{}catch{} catch blocks add NO overhead to code,
> > unless the exception actually gets thrown. But exceptions are
> > supposed to be
> > exceptional, so who cares how slow it is ?
> > ....
>
> Times for attached program (JDK1.2pre2, glibc2.1, RH6, 200MHZ PPro,
> 500000000 iterations):
>
>                 No JIT  sunwjit  tya
>                 ------  -------  ---
> With try/catch  278793   17925  33894
> Without         237905   15364  35803
>
> %slower          17.2%   16.7%  -5.3%
>
> I'd be surprised to see a zero-overhead try/catch, although tya seems to
> be on to something :-).
>
> Results are unaffected by -O compilation.
>
> Nathan

Ok, NO overhead for try..catch is an exaggeration. But only a small one
- typically there is exactly one extra bytecode instruction per try,
to branch round the catch{} clauses. Most C++ compilers produce similar
code,
and it would be easy for an optimizing java compiler to produce code
without even this overhead (although I haven't seen one that does so yet).

The byte-code produced by jdk1.2pre-v2 for the Hello program is reproduced
below.
Suns JDK 1.2 produces exactly the same code (of course!).
The extra instructions for the try..catch are marked with a !
Theres just 3 of them, and only one gets executed if the exception is not
thrown.

I guess the question was whether try..catch is faster or slower
than the alternatives. I think the program below is more
like the sort of situation that occurs in real life.

The times for this (Pentium 166, Sun Windows 95 JDK 1.2, Blackdown 1.2pre-v2
+ glibc 2.1 + OpenLinux 2.2,
500,000,000 iterations):

          VM:       kaffe    Sun        Sun    Blackdown    Blackdown
          JIT:               symcjit    none   sunwjit        none
                    -----    -------    ----   ---------    ----------
With try/catch    388,212    12,360    545,410    136,207    466,300
Without           396,401    15,490    601,000    143,784    508,770

%faster                2%        20%      9.2%       5.2%      8.3%

- the last column is estimated from 50,000,000 iterations.

I didn't believe the Sun VM figures either, but I tried both tests
several times, and the figures are consistent, scale linearly with
the number of iterations, and match wall-clock time. So it looks
like symcjit is pretty smart !

Nick

-----------------------------------------
import java.util.*;

public class Hello2 {

    public static void main(String[] argv) throws Exception {

        int count = Integer.parseInt(argv[0]);
        Boolean foobar = new Boolean(false);
        Date date1 = new Date();

        for (int i = 0; i < count;) {
            try {
                if (!foobar.booleanValue()) i++;
            }
            catch (Exception e) {
               System.err.println( "Oops!" );
            }
        }
        Date date2 = new Date();

        Date date3 = new Date();

        for (int i = 0; i < count;) {
            if(foobar != null) {
               if (!foobar.booleanValue()) i++;
            }
            else {
               System.err.println( "Oops!" );
            }
        }
        Date date4 = new Date();

        System.out.println("First loop (try): " +
                           (date2.getTime() - date1.getTime()));
        System.out.println("Second loop (no try): " +
                           (date4.getTime() - date3.getTime()));
    }
}
-----------------------------------------
Bytecode from Nathans Hello.java:
Code Disassembly:

   Addr Op Operands Code
   ---- -- -------- ----

...............
...............
   0011 03          iconst_0                    // for( int i = 0; .....
   0012 36 04       istore L4                   // (store i)
   0014 a7 001b     goto +1b = 002f
        try #1
   0017 84 0401     iinc L4,1                   // {   i++;
   001a 1d          iload_3
   001b 99 000b     ifeq +b = 0026              //     if( foobar ) ...
   001e bb 0005     new #5 <Class Exception>
   0021 59          dup
   0022 b7 000b     invokespecial #11 <Method void Exception.<init>()>
   0025 bf          athrow                      //     throw ...
   0026 1d          iload_3                     //     foobar = foobar &&
true;
   0027 3e          istore_3
        end try #1
!   0028 a7 0007     goto +7 = 002f              //     branch around catch
        catch #1 (Exception)
!   002b 57          pop                         //     start catch clause
!   002c a7 0003     goto +3 = 002f              //     end catch clause
   002f 15 04       iload L4                    // }
   0031 1b          iload_1
   0032 a1 ffe5     if_icmplt -1b = 0017        // end for loop 1
...............
...............
   0047 03          iconst_0                   // for( int i = 0; .....
   0048 36 07       istore L7                  // (store i)
   004a a7 0014     goto +14 = 005e
   004d 84 0701     iinc L7,1                  // {   i++;
   0050 1d          iload_3
   0051 99 000b     ifeq +b = 005c             //     if( foobar ) ...
   0054 bb 0005     new #5 <Class Exception>
   0057 59          dup
   0058 b7 000b     invokespecial #11 <Method void Exception.<init>()>
   005b bf          athrow                     //     throw ...
   005c 1d          iload_3
   005d 3e          istore_3                   //     foobar = foobar &&
true;
   005e 15 07       iload L7                   // }
   0060 1b          iload_1
   0061 a1 ffec     if_icmplt -14 = 004d       // end for loop 2
...............

-----------------------------------------


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to