Created
January 19, 2019 00:17
-
-
Save brendanmckenzie/2075cd06f60db7eba2c89acc009e8a9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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