Real life cost of using exceptions for control flow?

This is a discussion on Real life cost of using exceptions for control flow? within the Framework and Interface Programming forums in category; Hi. I'm working with an app that uses exceptions for control flow. These are code blocks where exceptions are thrown/caught regularly. A couple hundred exceptions occur per hour and they're caught close to the point of origination. I'm trying to decide whether to refactor... What is the cost of throwing an exception in the CLR - relative to, say, a conditional statement? Are we taking talking 1+ orders of magnitude? Is there significant churn in the CLR? Are my timers and threads going to suffer drift or unnecessary swaps each time an exception get fired? Thanks for anyone's input. I've ...

Go Back   Application Development Forum > Framework and Interface Programming

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-10-2004, 06:37 PM
Miyra
Guest
 
Default Real life cost of using exceptions for control flow?

Hi. I'm working with an app that uses exceptions for control flow.
These are code blocks where exceptions are thrown/caught regularly. A
couple hundred exceptions occur per hour and they're caught close to
the point of origination. I'm trying to decide whether to refactor...

What is the cost of throwing an exception in the CLR - relative to,
say, a conditional statement? Are we taking talking 1+ orders of
magnitude? Is there significant churn in the CLR? Are my timers and
threads going to suffer drift or unnecessary swaps each time an
exception get fired?

Thanks for anyone's input. I've spent time snooping the news groups
and csharp.net, but haven't run into a great answer yet (besides
"don't use exceptions for control flow" arguments :-)

-Miyra

ps: Developing with MS Visual C# NET.

ps: Here are some links I found
http://msdn.microsoft.com/library/de...etperftips.asp
http://msdn.microsoft.com/library/de...anagedapps.asp
http://msdn.microsoft.com/library/de...netchapt15.asp
Reply With Quote
  #2  
Old 06-10-2004, 06:41 PM
Jon Skeet [C# MVP]
Guest
 
Default Re: Real life cost of using exceptions for control flow?

Miyra <miyra70@yahoo.com> wrote:
> Hi. I'm working with an app that uses exceptions for control flow.
> These are code blocks where exceptions are thrown/caught regularly. A
> couple hundred exceptions occur per hour and they're caught close to
> the point of origination. I'm trying to decide whether to refactor...
>
> What is the cost of throwing an exception in the CLR - relative to,
> say, a conditional statement? Are we taking talking 1+ orders of
> magnitude? Is there significant churn in the CLR? Are my timers and
> threads going to suffer drift or unnecessary swaps each time an
> exception get fired?
>
> Thanks for anyone's input. I've spent time snooping the news groups
> and csharp.net, but haven't run into a great answer yet (besides
> "don't use exceptions for control flow" arguments :-)


Certainly it's not worth refactoring just because of the performance
hit of a couple of hundred exceptions per hour. My laptop can throw and
catch hundreds of *thousands* of exceptions per *second*.

So, ignore the performance aspect for the moment, and design it in the
most readable and natural way.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply With Quote
  #3  
Old 06-10-2004, 08:20 PM
Jerry Pisk
Guest
 
Default Re: Real life cost of using exceptions for control flow?

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.

Jerry

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1b32f3db81295ca198abf4@msnews.microsoft.c om...
> Miyra <miyra70@yahoo.com> wrote:
>> Hi. I'm working with an app that uses exceptions for control flow.
>> These are code blocks where exceptions are thrown/caught regularly. A
>> couple hundred exceptions occur per hour and they're caught close to
>> the point of origination. I'm trying to decide whether to refactor...
>>
>> What is the cost of throwing an exception in the CLR - relative to,
>> say, a conditional statement? Are we taking talking 1+ orders of
>> magnitude? Is there significant churn in the CLR? Are my timers and
>> threads going to suffer drift or unnecessary swaps each time an
>> exception get fired?
>>
>> Thanks for anyone's input. I've spent time snooping the news groups
>> and csharp.net, but haven't run into a great answer yet (besides
>> "don't use exceptions for control flow" arguments :-)

>
> Certainly it's not worth refactoring just because of the performance
> hit of a couple of hundred exceptions per hour. My laptop can throw and
> catch hundreds of *thousands* of exceptions per *second*.
>
> So, ignore the performance aspect for the moment, and design it in the
> most readable and natural way.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Reply With Quote
  #4  
Old 06-10-2004, 09:21 PM
Alvin Bruney [MVP]
Guest
 
Default Re: Real life cost of using exceptions for control flow?

> Certainly it's not worth refactoring just because of the performance
> hit of a couple of hundred exceptions per hour. My laptop can throw and
> catch hundreds of *thousands* of exceptions per *second*.


You decide based on the facts:
Here are the results. Time in milliseconds

with Exception
start 0.002113 0.001080
stop 0.007242 0.005129

with conditional check
start 0.001568 0.000545
stop 0.001634 0.000066

with control
start 0.001581 0.000533
stop 0.001638 0.000056
=====================================

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{

// Exception
string i = string.Empty;
Trace.Write("start");
try
{
throw new Exception("horrid idea");
}
catch
{
}
Trace.Write("stop");


//Control
Trace.Write("start");
Trace.Write("stop");

//With check
string i = string.Empty;
Trace.Write("start");

if(true == true)
i = string.Empty;

Trace.Write("stop");

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1b32f3db81295ca198abf4@msnews.microsoft.c om...
> Miyra <miyra70@yahoo.com> wrote:
>> Hi. I'm working with an app that uses exceptions for control flow.
>> These are code blocks where exceptions are thrown/caught regularly. A
>> couple hundred exceptions occur per hour and they're caught close to
>> the point of origination. I'm trying to decide whether to refactor...
>>
>> What is the cost of throwing an exception in the CLR - relative to,
>> say, a conditional statement? Are we taking talking 1+ orders of
>> magnitude? Is there significant churn in the CLR? Are my timers and
>> threads going to suffer drift or unnecessary swaps each time an
>> exception get fired?
>>
>> Thanks for anyone's input. I've spent time snooping the news groups
>> and csharp.net, but haven't run into a great answer yet (besides
>> "don't use exceptions for control flow" arguments :-)

>
> Certainly it's not worth refactoring just because of the performance
> hit of a couple of hundred exceptions per hour. My laptop can throw and
> catch hundreds of *thousands* of exceptions per *second*.
>
> So, ignore the performance aspect for the moment, and design it in the
> most readable and natural way.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Reply With Quote
  #5  
Old 06-10-2004, 10:05 PM
Guest
 
Default Re: Real life cost of using exceptions for control flow?

It is a known fact and should be emphasized that Exceptions should be
avoided at all costs.
If you can do a conditional check to avoid throwing the exception then 99
times out of 100 that is the best way to go. And better in more ways then
one. Not only does it offer better performance but it also makes the
developers and auditors more aware of the possible errors that could be
thrown. It may make somebody aware of something that needs to be handled in
a more delicate way.



"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:OQgylJ1TEHA.2324@TK2MSFTNGP10.phx.gbl...
>> Certainly it's not worth refactoring just because of the performance
>> hit of a couple of hundred exceptions per hour. My laptop can throw and
>> catch hundreds of *thousands* of exceptions per *second*.

>
> You decide based on the facts:
> Here are the results. Time in milliseconds
>
> with Exception
> start 0.002113 0.001080
> stop 0.007242 0.005129
>
> with conditional check
> start 0.001568 0.000545
> stop 0.001634 0.000066
>
> with control
> start 0.001581 0.000533
> stop 0.001638 0.000056
> =====================================
>
> using System;
> using System.Collections;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Web;
> using System.Web.SessionState;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.HtmlControls;
>
> namespace WebApplication2
> {
> /// <summary>
> /// Summary description for WebForm1.
> /// </summary>
> public class WebForm1 : System.Web.UI.Page
> {
> private void Page_Load(object sender, System.EventArgs e)
> {
>
> // Exception
> string i = string.Empty;
> Trace.Write("start");
> try
> {
> throw new Exception("horrid idea");
> }
> catch
> {
> }
> Trace.Write("stop");
>
>
> //Control
> Trace.Write("start");
> Trace.Write("stop");
>
> //With check
> string i = string.Empty;
> Trace.Write("start");
>
> if(true == true)
> i = string.Empty;
>
> Trace.Write("stop");
>
> }
>
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
>
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
> #endregion
> }
> }
>
> --
> Regards,
> Alvin Bruney
> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
> Got tidbits? Get it here... http://tinyurl.com/27cok
> "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
> news:MPG.1b32f3db81295ca198abf4@msnews.microsoft.c om...
>> Miyra <miyra70@yahoo.com> wrote:
>>> Hi. I'm working with an app that uses exceptions for control flow.
>>> These are code blocks where exceptions are thrown/caught regularly. A
>>> couple hundred exceptions occur per hour and they're caught close to
>>> the point of origination. I'm trying to decide whether to refactor...
>>>
>>> What is the cost of throwing an exception in the CLR - relative to,
>>> say, a conditional statement? Are we taking talking 1+ orders of
>>> magnitude? Is there significant churn in the CLR? Are my timers and
>>> threads going to suffer drift or unnecessary swaps each time an
>>> exception get fired?
>>>
>>> Thanks for anyone's input. I've spent time snooping the news groups
>>> and csharp.net, but haven't run into a great answer yet (besides
>>> "don't use exceptions for control flow" arguments :-)

>>
>> Certainly it's not worth refactoring just because of the performance
>> hit of a couple of hundred exceptions per hour. My laptop can throw and
>> catch hundreds of *thousands* of exceptions per *second*.
>>
>> So, ignore the performance aspect for the moment, and design it in the
>> most readable and natural way.
>>
>> --
>> Jon Skeet - <skeet@pobox.com>
>> http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too

>
>



Reply With Quote
  #6  
Old 06-11-2004, 03:04 AM
Jon Skeet [C# MVP]
Guest
 
Default Re: Real life cost of using exceptions for control flow?

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
Reply With Quote
  #7  
Old 06-11-2004, 03:06 AM
Jon Skeet [C# MVP]
Guest
 
Default Re: Real life cost of using exceptions for control flow?

<johndoe@driver.net> wrote:
> It is a known fact and should be emphasized that Exceptions should be
> avoided at all costs.


No - not at *all* costs. Not at the cost of a clean design. Not at the
cost of performance in other ways, just going on the principle of
"never throw exceptions because they kill performance".

If exceptions were to be avoided at all costs, we wouldn't have them -
we'd still be using return codes.

Yes, exceptions should be avoided for unexceptional circumstances, but
that's *not* the same thing as "avoided at all costs".

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply With Quote
  #8  
Old 06-11-2004, 03:09 AM
Jon Skeet [C# MVP]
Guest
 
Default Re: Real life cost of using exceptions for control flow?

<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
> > Certainly it's not worth refactoring just because of the performance
> > hit of a couple of hundred exceptions per hour. My laptop can throw and
> > catch hundreds of *thousands* of exceptions per *second*.

>
> You decide based on the facts:
> Here are the results. Time in milliseconds
>
> with Exception
> start 0.002113 0.001080
> stop 0.007242 0.005129
>
> with conditional check
> start 0.001568 0.000545
> stop 0.001634 0.000066
>
> with control
> start 0.001581 0.000533
> stop 0.001638 0.000056


Right - and a couple of hundred of those an hour are going to make how
much difference, exactly? In the case of it increasing the time by
0.005ms, that'll make a whole millisecond of difference over an hour.

If you want to bend designs out of shape for that performance increase,
go ahead - I would recommend against it.

That's not to say that using exceptions is the right way to go here -
but performance *isn't* the main factor to consider by a long stretch.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply With Quote
  #9  
Old 06-11-2004, 03:36 AM
=?ISO-8859-1?Q?Michel_Andr=E9?=
Guest
 
Default Re: Real life cost of using exceptions for control flow?

Miyra wrote:
> Hi. I'm working with an app that uses exceptions for control flow.
> These are code blocks where exceptions are thrown/caught regularly. A
> couple hundred exceptions occur per hour and they're caught close to
> the point of origination. I'm trying to decide whether to refactor...


I wouldn't worry about performance implications.

But I would consider it bad design and usually quite bad code thats hard
to read if exceptions are used as the programs main flow of control and
not used for exceptional and error situations. There are many
discussions and advises about when to use exceptions or not.

/Michel
Reply With Quote
  #10  
Old 06-11-2004, 10:21 AM
Alvin Bruney [MVP]
Guest
 
Default Re: Real life cost of using exceptions for control flow?

that's what i was going to say but you said it rather well.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"Michel André" <michel.andre@swipnet.se> wrote in message
news:2it5njFrdf55U1@uni-berlin.de...
> Miyra wrote:
>> Hi. I'm working with an app that uses exceptions for control flow. These
>> are code blocks where exceptions are thrown/caught regularly. A
>> couple hundred exceptions occur per hour and they're caught close to
>> the point of origination. I'm trying to decide whether to refactor...

>
> I wouldn't worry about performance implications.
>
> But I would consider it bad design and usually quite bad code thats hard
> to read if exceptions are used as the programs main flow of control and
> not used for exceptional and error situations. There are many discussions
> and advises about when to use exceptions or not.
>
> /Michel



Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:28 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.