harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anton Avtamonov" <anton.avtamo...@gmail.com>
Subject Re: More helpful error messages
Date Mon, 27 Mar 2006 09:18:54 GMT
I was so impressed by the results that decided to reproduce them.
No wonder I got very similar timing :-)

I slightly modified test to be able to see how much time Exception
object creation takes itself:


public class Test {
   private static long exceptionCreationTime;
   private static long exceptionThrowMoment;
   private static long exceptionThrowTime;

   public static void main(final String[] args) {
       long startTime = System.currentTimeMillis();
       for( int i = 0; i < 500000; i++ ) {
           try {
               exceptionalMethod();
           } catch (Exception e) {
               exceptionThrowTime += System.currentTimeMillis() -
exceptionThrowMoment;
           }
       }
      long totalTime = System.currentTimeMillis() - startTime;
      System.out.println("total time = " + totalTime);
      System.out.println("exception creation time = " + exceptionCreationTime);
      System.out.println("exception throwing time = " + exceptionThrowTime);
      System.out.println("rest time = " + (totalTime -
exceptionCreationTime - exceptionThrowTime));
   }

   static void exceptionalMethod() {
       long startTime = System.currentTimeMillis();
       RuntimeException e = new RuntimeException();
       exceptionCreationTime += (System.currentTimeMillis() - startTime);
	
       exceptionThrowMoment = System.currentTimeMillis();
       throw e;
   }
}

I got the following (in average):

total time = 2743
exception creation time = 2252
exception throwing time = 341
rest time = 150

Really, Exceptions creation tales too much and (suprising for me) not
too much when throwing. Probably that is because in this sample stack
frame is not huge and if we have bigger method call stack exception
throwing will take more?

I tried to increase stack (I'm not sure how adequate this test is):

public class Test2 {
   private static long exceptionCreationTime;
   private static long exceptionThrowMoment;
   private static long exceptionThrowTime;

   public static void main(final String[] args) {
       long startTime = System.currentTimeMillis();
       for( int i = 0; i < 500000; i++ ) {
           try {
               exceptionalMethod(100);
           } catch (Exception e) {
               exceptionThrowTime += System.currentTimeMillis() -
exceptionThrowMoment;
           }
       }
       long totalTime = System.currentTimeMillis() - startTime;
       System.err.println("total time = " + totalTime);
       System.err.println("exception creation time = " + exceptionCreationTime);
       System.err.println("exception throwing time = " + exceptionThrowTime);
       System.err.println("rest time = " + (totalTime -
exceptionCreationTime - exceptionThrowTime));
   }

   static void exceptionalMethod(final int counter) {
       if (counter > 0) {
           exceptionalMethod(counter - 1);
       } else {
           exceptionalMethodInner();
       }
   }

   static void exceptionalMethodInner() {
       long startTime = System.currentTimeMillis();
       RuntimeException e = new RuntimeException();
       exceptionCreationTime += (System.currentTimeMillis() - startTime);
	
       exceptionThrowMoment = System.currentTimeMillis();
       throw e;
   }
}

The results are:

total time = 20400
exception creation time = 16805
exception throwing time = 2623
rest time = 972

So even in such conditions Exception object creation is dominating.

Anyway, absolutely agree with Geir: exceptions should not be used for
'normal' operation results where possible (i.e. where not defined by
spec or RI).

--
Anton Avtamonov,
Intel Middleware Products Division

Mime
View raw message