Performance of Java exceptions
Influence of exception on the application performance
The first converter uses exceptions to determine if a value is not a Long value.
{% highlight java }
try {
value = Long.parseLong(toParse.trim());
} catch (NumberFormatException e) {
isLong = false;
}
{ endhighlight %}
The second example evaluates if the String contains of digits only.
{% highlight java %}
char buf[] = toParse.trim().toCharArray();
{% endhighlight %}
With a String of “123456a” the second approach is more than 10 times faster. A shorter parse string requires even less time.
It is important to know that the time needed per thousand runs is only 2 milliseconds for the exception version. In most cases this should play no importance.
Kind Regards / Viele Grüße
Sebastian Hennebrueder