try...catch slooowness?

Lutger Blijdestijn lutger.blijdestijn at gmail.com
Sun Dec 19 05:20:37 PST 2010


spir wrote:

> Hello,
> 
> 
> I had not initially noticed that the 'in' operator (for AAs) returns a
> pointer to the looked up element. So that, to avoid double lookup in cases
> where lookups may fail, I naively used try...catch. In cases of very
> numerous lookups, my code suddenly became blitz fast. So that I wondered
> about exception handling efficiency. Below a test case (on my computer,
> both loops run in about the same average time):
> 

Catching exceptions is known to be a bit costly. Generally, in C family 
languages with exceptions (such as D), it is considered bad practice to use 
exceptions for regular control flow. The cost is therefore acceptable 
because it is not suffered on the normal, happy code path. 

That the performance cost is so huge is likely not only the inherent 
overhead of exceptions, but probably also some extra cache misses or 
something like that.[needs analysis]



More information about the Digitalmars-d mailing list