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 ?
> 
> Check out how exception handling is implemented in Java byte-code
> in the Java Virtual Machine Specification, 4.7.3 - The Code
> Attribute.


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

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

import java.util.*;

public class Hello
{
    public static void main(String[] argv) throws Exception
    {
        int count = Integer.parseInt(argv[0]);
        Date date1 = new Date();
        boolean foobar = false;
        for (int i = 0; i < count;)
        {
            try
            {
                i++;
                if (foobar) throw new Exception();
                // Try to make optimization-resistant :-)
                foobar = foobar && true;
            }
            catch (Exception e) { }
        }
        Date date2 = new Date();
        Date date3 = new Date();
        for (int i = 0; i < count;)
        {
            i++;
            if (foobar) throw new Exception();
            // Try to make optimization-resistant :-)
            foobar = foobar && true;
        }
        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()));
    }
}

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


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

Reply via email to