Skip to content

Instantly share code, notes, and snippets.

@brendanmckenzie
Created January 19, 2019 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendanmckenzie/2075cd06f60db7eba2c89acc009e8a9a to your computer and use it in GitHub Desktop.
Save brendanmckenzie/2075cd06f60db7eba2c89acc009e8a9a to your computer and use it in GitHub Desktop.
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