using System;
using System.Diagnostics;
using System.Globalization;
namespace ConsoleApplication18
{
class Program
{
static void Main()
{
const int maxTimes = 5000;
const string inputString = "2xc61";
CodeTime("TryParse", () =>
{
for (int i = 0; i < maxTimes; i++)
{
int t;
if (!int.TryParse(inputString, out t))
{
//Console.WriteLine("xxxx");
}
}
});
CodeTime("TryCatch", () =>
{
for (int i = 0; i < maxTimes; i++)
{
try
{
int t = int.Parse(inputString);
}
catch (Exception)
{
//Console.WriteLine("xxxxxxxxx");
}
}
});
Console.WriteLine("Over!");
Console.ReadKey();
}
static void CodeTime(string title, Action action)
{
var watcher = Stopwatch.StartNew();
Console.WriteLine(title + " Start...");
action();
Console.WriteLine("Finish...");
Console.WriteLine("Time: " + watcher.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture) + "ms");
Console.WriteLine();
}
}
}
TryParse Start...
Finish...
Time: 1ms
TryCatch Start...
Finish...
Time: 24938ms
Over!