Performance of exception handling

Walter Bright newshound2 at digitalmars.com
Mon May 9 21:20:43 PDT 2011


On 5/9/2011 8:09 PM, Andrej Mitrovic wrote:
> I'll have to agree that exceptions are quite slow. I was just testing
> out UTF's decode() function on this UTF-8 test file:
> http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt .
>
> It has a few dozen invalid UTF sequences and is a good stress-test for
> a decoder. The  std.utf.decode function throws an exception on invalid
> UTF sequences.
>
> On that test file (only 20 kilobytes large), loading of a file and
> skipping invalid sequences takes 210msecs.
>
> If I change decode() to use a bool to flag invalid sequences instead
> of using exceptions, the UTF test file is parsed in 1.4msecs. Now
> that's quite a difference.


Validating input data should not be done with exceptions. The exceptions should 
be thrown only for data that is *expected* to be correct.


More information about the Digitalmars-d mailing list