Re: Real life cost of using exceptions for control flow?

From: Jerry Pisk (jerryiii_at_hotmail.com)
Date: 06/11/04


Date: Fri, 11 Jun 2004 13:28:37 -0700

I agree, that if we're talking about throwing several hundreds of exceptions
an hour there's no performance issue. However, exceptions are very expensive
compared to value checks (i.e. the cost of Param == null is pretty close to
zero compared to throwing an exception) so if you're writing performance
critical code (such as drawing code mentioned by Frank, or web application)
you should avoid exceptions at any cost. And you need to realize that ANY
web application is performance critical, since poor performance can very
easily lead to DoS on even the least used server.

And if I had to generalize I would say the threshold would be somewhere
around 1,000 exceptions a second, not hundreds (or even tens) of thousands.

Jerry

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1b3369db8d796a2198abf6@msnews.microsoft.com...
> Jerry Pisk <jerryiii@hotmail.com> wrote:
>> Jon, could you please post the code that throws and catches hundreds of
>> thousands exceptions per second? My box (dual PIII @ 850) can only run
>> about
>> 35,000 to 40,000 of the following in a second:
>>
>> try
>> {
>> throw new ApplicationException("Just testing.");
>> }
>> catch(Exception)
>> {
>> }
>>
>> I would like to see code that can throw and catch exceptions ten times as
>> fast.
>
> using System;
>
> public class Test
> {
> static void Main()
> {
> DateTime start = DateTime.Now;
> for (int i=0; i < 1000000; i++)
> {
> try
> {
> throw new Exception();
> }
> catch
> {
> }
> }
> DateTime end = DateTime.Now;
> Console.WriteLine (end-start);
> }
> }
>
> That's a million exceptions, and on my laptop (P4/3GHz) it takes 9.25
> seconds. So maybe "hundreds of thousands" should have been "over a
> hundred thousand" - but it's still enough that 200 per hour isn't going
> to be an issue :)
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Relevant Pages

  • Re: Is MCSD worth doing?
    ... They don't even need to catch exceptions but finish ... incompetent or fraudulent use of software written by 'users', ... the short term gain in cost ... People simply fail to realize that producing high quality software is ...
    (microsoft.public.cert.exam.mcsd)
  • Re: Is Letter Sent by Email Considered to Be Legal?
    ... the condition is already met and he/she must cover the cost as stated? ... An email letter has the same binding effect as any other letter, ... exceptions, but I am not claiming that there are only three. ... effect negated by limitation or authority. ...
    (misc.legal)
  • Re: the 100W bulb lives on....
    ... In article, Phil Hobbs wrote: ... I mention the many exceptions, and a link to the relevant ... some GE halogens already meet ... including electricity cost at USA ...
    (sci.electronics.design)
  • Re: Real life cost of using exceptions for control flow?
    ... exceptions per hour shouldn't be worried about in terms of performance ... It takes *very* poor reasoning to go from that to thinking I advocate ... of a clean design than for the sake of performance. ... Performance at any cost, ...
    (microsoft.public.dotnet.framework.performance)
  • Re: New Telefunkens
    ... It is an argument that makes a business case for a professional recording location to own the tools that make it more efficient than others, ... I did not restrict myself to a single case, other than to state that the expensive microphone is a only small part of the cost of running a very expensive chain of components. ... exceptions to any claim, but if the exceptions are very few, we can ...
    (rec.audio.pro)