Exceptions and execution speed

This post was triggered by a comment by Alex Hunsley on the Multithreaded Game Scripting post.

Alex correctly points out that while setting up a “try” for an exception costs nothing, raing an exception does! The question however is whether using exceptions is a speed overkill in the altar of code readability

It turns out that once again the answer is the wildcard answer : “It depends!…”

Lets examine the two posibilities for getting a value from a dictionary in python. First lets look at the version that uses exceptions :

    value = data[key]
except KeyError:
    value = default

And the equivalent without exceptions :

if key in data:
    value = data[key]
    value = default

If the key is the dictionary 99% of the time then the version that uses exceptions is the fastest you can do. There is no overhead to check if the key is in. While the second version will waste time checking for something that will 99% be there.

However if the is not likely to by the dictionary the cost of raising exceptions will make the second version faster.

So when an exception is a non common thing to occur, it is wise to use exceptions! In a game most of the events happen rarely compared to how many time a piece of code runs. So exceptions as events will not only make code easier to read, but faster too, since it will remove unecessary checks.

  • Yeah, the exception version may be faster (although it’s quite possible that it isn’t), and I actually think the other one is easier to read. But you are not answering the main point of Alex Hunsley’s comment, which is that exceptions are meant to be used for signalling unexpected circumstances (hence the name), not as a means for flow control. And doing things the way it’s normally done is an important notion in programming, known as “the rule of least astonishment”. Still, your original example with the closing door was a better example of a situation when you could use exceptions in this novel way.

  • The post wasn’t meant to answer to Alex. It was just triggered by his post. I agree with you on doing things the normal way, but the purpose of the article was to create a new normal way.

    This post is only about the speed part of the matter…

  • Mincetro

    It doesn’t sound like it would save much time, but I’d never really used exceptions. I can actually see the exception method working faster if it is what I think it is.

  • Exceptions are not safe to be used as part of normal logic. In fact, in C++ this would not be the way to go due to these factors: There is a performance overhead even for a try block. Raising the exception cost a helluva lot of speed Fucked up win32 model (SEH + C++ exception can really ruin your day) Release VC++ programs do not retain them. Therefore, the above is not portable to C++ I am not familiar with Java/C#/python/interpreted language of choice exception model but at best this is a party trick 🙁 Cheers for trying something new though

  • Pingback: no.name()()

  • Raydeejay

    After reading ThanasisK’s comment, I’ve finally come to realize that VC++ is THE reference implementation of the C++ language 🙂

  • VC++ is not the reference implementation, vc6 was f*****d up. But how about a “de facto” standard?

  • Please ignore last comment