V
V
Vladimir Golovanov2011-11-30 12:11:46
Java
Vladimir Golovanov, 2011-11-30 12:11:46

How expensive is throw?

It is known that the instruction new Exception()in Java is quite expensive due to the collection of the stacktrace.
However, sometimes in code that needs to complete in a fixed time, you want to throw an exception StopProcessing (extends RuntimeException)that will be caught at the top level.
You can avoid the stacktrace overhead by first creating an Exception and storing it in a static final variable, because only the fact of stopping is of interest.
The question is: how much more expensive (and will it be) to do a throw instead of a chain of returns with a check of the stop flags?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
senia, 2011-11-30
@senia

Recently there were articles just on this topic:
Part time
Part two

A
Anatoly, 2011-11-30
@taliban

Of course, the returns will be faster, but are they worth the speed? Exceptions are much nicer and more convenient to work with.

V
Vladimir Golovanov, 2011-11-30
@Colwin

Wrote a little test:


public class Test
{
    public static final RuntimeException fex = new RuntimeException();

    public static void main(String[] args) throws Exception {
        long start;
        long end;

        start = System.currentTimeMillis();

        for (int i = 0; i < 10000; i++) {
            testReturn(0);
        }

        end = System.currentTimeMillis();

        System.out.println("Return time: " + (end - start) + "ms");

        start = System.currentTimeMillis();

        for (int i = 0; i < 10000; i++) {
            try {
                testExceptionThrow(0);
            } catch (RuntimeException e) {
            }
        }

        end = System.currentTimeMillis();

        System.out.println("Exception throw time: " + (end - start) + "ms");

        start = System.currentTimeMillis();

        for (int i = 0; i < 10000; i++) {
            try {
                testExceptionCreate(0);
            } catch (RuntimeException e) {
            }
        }

        end = System.currentTimeMillis();

        System.out.println("Exception create time: " + (end - start) + "ms");
    }

    public static void testReturn(int count) {
        if (count > 1000) {
            return;
        } else {
            testReturn(count + 1);
        }
    }

    public static void testExceptionThrow(int count) {
        if (count > 1000) {
            throw fex;
        } else {
            testExceptionThrow(count + 1);
        }
    }

    public static void testExceptionCreate(int count) {
        if (count > 1000) {
            throw new RuntimeException();
        } else {
            testExceptionCreate(count + 1);
        }
    }
}

As a result, we have the following figures: The test, of course, does not claim to be highly accurate, but it shows the order of magnitude.
Return time: 15ms
Exception throw time: 203ms
Exception create time: 1079ms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question