Try-catch performance
Ukázka, že try-catch block opravdu zatěžuje cpu více.
java
using System;
using System.Diagnostics;
class Program
{
[STAThread]
static void Main(string[] args)
{
int N = 5000;
Stopwatch s1 = Stopwatch.StartNew();
for (int i = 0; i < N; i++)
{
A();
}
s1.Stop();
Console.WriteLine(s1.ElapsedTicks);
Stopwatch s2 = Stopwatch.StartNew();
for (int i = 0; i < N; i++)
{
B();
}
s2.Stop();
Console.WriteLine(s2.ElapsedTicks);
Stopwatch s3 = Stopwatch.StartNew();
for (int i = 0; i < N; i++)
{
C();
}
s3.Stop();
Console.WriteLine(s3.ElapsedTicks);
}
static void A()
{
try
{
string a = "5";
int aa = int.Parse(a);
}
catch (Exception)
{
throw;
}
}
static void B()
{
string b = "5";
int bb = int.Parse(b);
}
static void C()
{
try
{
string c = "s";
int cc = int.Parse(c);
}
catch (Exception)
{
}
}
}
Neformátovaný
Přidáno: 23.9.2013
Expirace: Neuvedeno