| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| 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 |
|
#4
| |||
| |||
| > 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 |
|
#5
| |||
| |||
| 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 > > |
|
#6
| |||
| |||
| 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 |
|
#7
| |||
| |||
| <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 |
|
#8
| |||
| |||
| <"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 |
|
#9
| |||
| |||
| 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 |
|
#10
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.