Performance, how heavy a try / catch? (. NET)
These days are entered into a discussion where we began to assess the "weight" of a try / catch block.
If a pure block try / catch, not used does not cost anything to use it incorrectly has an enormous weight.
To demonstrate this I used a code profiler and small. NET code that contained these two functions:
public static int NoCatch (object date) {
if (date != null)
{ return (int)data; }
else
{ return 1; }
}
public static int WithCatch(object data)
{
try
{
return (int)data;
} catch
{return 1;
}}
As you can see the function WithCatch (object data) uses the try to run a generic error condition, while the first block of code uses a simple if to avoid the error.
Well, what were the results? Here they are:
TryCatchTest.Program.TestNoCatch (int64, object): 0.17
TryCatchTest.Program.TestWithCatch (int64, object): 491.77
Well ... up to 10,000 processing method, nearly% of the time was used by the second function.
0 comments:
Post a Comment