Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Diagnostics;
using System.Linq;
namespace ExceptionTest
{
class Program
{
const int SampleSize = 1000000;
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
Console.WriteLine("Testing with exceptions");
stopwatch.Start();
ThrowsExceptions();
stopwatch.Stop();
Console.WriteLine($"Time elapsed: {stopwatch.ElapsedMilliseconds}ms");
stopwatch.Reset();
Console.WriteLine("Testing without exceptions");
stopwatch.Start();
DoesNotThrowExceptions();
stopwatch.Stop();
Console.WriteLine($"Time elapsed: {stopwatch.ElapsedMilliseconds}ms");
}
static void ThrowsExceptions()
{
Enumerable.Range(0, SampleSize).AsParallel().ForAll(ent =>
{
try
{
DateTime.Parse("INVALID DATE");
}
catch (FormatException)
{
}
});
}
static void DoesNotThrowExceptions()
{
Enumerable.Range(0, SampleSize).AsParallel().ForAll(ent =>
{
DateTime.TryParse("INVALID DATE", out var res);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.