BenchTryCatch.cs
Porovnání rychlosti try..catch a explicitního testu
java
using System;
using System.Diagnostics;
class BenchTryCatch {
static void Main(string[] args) {
Random rnd = new Random();
int delitel=rnd.Next(5)+1;
{
int suma = 0;
Stopwatch s1 = Stopwatch.StartNew();
for (int i=0; i<10000000; i++) {
try {
suma += i/delitel;
} catch {
Console.WriteLine("Chyba");
}
}
s1.Stop();
Console.WriteLine(s1.ElapsedTicks);
}
{
int suma = 0;
Stopwatch s1 = Stopwatch.StartNew();
for (int i=0; i<10000000; i++) {
if (delitel != 0) {
suma += i/delitel;
}
}
s1.Stop();
Console.WriteLine(s1.ElapsedTicks);
}
}
}
Neformátovaný
Přidáno: 23.9.2013
Expirace: Neuvedeno