[antlr-interest] how to do exceptions quickly

Alex Shneyderman a.shneyderman at gmail.com
Wed Apr 18 03:03:53 PDT 2007


Just tom make sure I understand you. Is this what you mean?

public class TestExceptions {
	
	public static void main(String[] args) {
		long start, end;
		start = System.currentTimeMillis();
		for (int i = 1; i <= TOTAL_ITERATIONS; i++) {
			try {
				throw new Exception ("");
			} catch (Exception e) {
			}
		}
		end = System.currentTimeMillis();
		System.out.println("[" + TOTAL_ITERATIONS + "] Total time with
non-static exceptions: " + (end - start));
		
		start = System.currentTimeMillis();
		for (int i = 1; i <= TOTAL_ITERATIONS; i++) {
			try {
				throw staticException;
			} catch (Exception e) {
			}
		}
		end = System.currentTimeMillis();
		System.out.println("[" + TOTAL_ITERATIONS + "] Total time with
static exceptions: " + (end - start));
	}
	
	private static int TOTAL_ITERATIONS = 1000000;
	private static Exception staticException = new Exception ();

}

the output
[1000000] Total time with non-static exceptions: 2662
[1000000] Total time with static exceptions: 155

If not could you elaborate on this a bit?

On 4/17/07, Terence Parr <parrt at cs.usfca.edu> wrote:
> Hi,
>
> I had a long conversation at Google last night with Neal Gafter, the
> guy who built the javac compiler when he was at Sun. He told me that
> exceptions can be made to execute very quickly.  The only thing you
> have to avoid is the actual exception object creation which has to do
> all of the expensive stack trace creation and so on.  He says that
> the actual throwing of the exception itself is not a problem.  This
> might be something to look at later to see if it goes quickly,
> because it results in cleaner backtracking code.  Anyway, we can
> create some singleton objects, which will solve the problem.  Hooray!
>
> Anyway, this might be useful to people in general.  I thought was
> pass along this interesting trick.
>
> He confirmed that -target jsr14 should be okay for us to use generics
> in the antler tool and generate 1.4 compatible.class files.  He is
> the author of all that, so I believe. ;)
>
> Ter


More information about the antlr-interest mailing list