Created
October 21, 2017 14:43
-
-
Save benaadams/ebea3a947a57fe04e9fa0ee8bed48956 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.Threading.Tasks; | |
| using System.Linq; | |
| public class Program | |
| { | |
| public static async Task Main(string[] args) | |
| { | |
| const int Iters = 100000; | |
| Console.WriteLine(await ThrowCatch()); | |
| var sw = Stopwatch.StartNew(); | |
| sw.Stop(); | |
| var prealloc = GC.GetAllocatedBytesForCurrentThread(); | |
| sw.Start(); | |
| for (int i = 0; i < Iters; i++) | |
| { | |
| await ThrowCatch(); | |
| } | |
| sw.Stop(); | |
| var totalBytes = GC.GetAllocatedBytesForCurrentThread()- prealloc; | |
| Console.WriteLine($"Total Iters: {Iters:N0} Elapsed: {sw.Elapsed} Bytes Allocated: {totalBytes}"); | |
| Console.WriteLine($"Ops Per Sec: {Iters/ sw.Elapsed.TotalSeconds:N1} Elapsed Per Op: {sw.Elapsed/Iters} Bytes Per Op: {totalBytes/Iters}"); | |
| } | |
| static async Task<Exception> ThrowCatch() | |
| { | |
| try | |
| { | |
| await MethodAsync(2); | |
| } | |
| catch (Exception e) | |
| { | |
| return e; | |
| } | |
| return null; | |
| } | |
| static List<int> _list = new List<int> { 1, 2, 3 }; | |
| private static bool FirstException(int value, int index) | |
| { | |
| foreach (var ii in _list) | |
| { | |
| _list.Add(ii - 1); | |
| } | |
| return true; | |
| } | |
| private static int LinqIteration(int count) | |
| { | |
| var sum = 0; | |
| var list = new List<int>{1,2,3}; | |
| var data = list.SkipWhile((x, i) => FirstException(x, i)).ToArray(); | |
| return data.Sum(); | |
| } | |
| private static IEnumerable<int> Iterator(long count, int value) | |
| { | |
| yield return 1; | |
| yield return LinqIteration(2); | |
| yield return 3; | |
| } | |
| private static int InnerException3() | |
| { | |
| try | |
| { | |
| int val = 0; | |
| foreach (var i in Iterator(1L, 1)) | |
| { | |
| val = i; | |
| } | |
| return val; | |
| } | |
| catch (Exception e) | |
| { | |
| throw new Exception("Inner3", e); | |
| } | |
| } | |
| struct InnerType<T> | |
| { | |
| public static int InnerException2(int count) | |
| { | |
| try | |
| { | |
| return InnerException3(); | |
| } | |
| catch (Exception e) | |
| { | |
| throw new Exception("Inner2", e); | |
| } | |
| } | |
| } | |
| private static int InnerException1<T>(int count) | |
| { | |
| try | |
| { | |
| return InnerType<int>.InnerException2(count); | |
| } | |
| catch (Exception e) | |
| { | |
| throw new Exception("Inner1", e); | |
| } | |
| } | |
| private static ValueTask<int> NonAsyncValueTask(int count, string name) | |
| { | |
| return new ValueTask<int>(InnerException1<string>(count)); | |
| } | |
| private static async ValueTask<int> ValueTaskAsync(string name) | |
| { | |
| return await NonAsyncValueTask(1, ""); | |
| } | |
| private static async Task<int> TaskTAsync(long count) | |
| { | |
| return await ValueTaskAsync(""); | |
| } | |
| private static async Task<int> TaskTAsync(int count) | |
| { | |
| return await ValueTaskAsync(""); | |
| } | |
| private static async Task<int> TaskTAsync(long count, string name) | |
| { | |
| return await ValueTaskAsync(""); | |
| } | |
| private static async Task ConfiguredAsync(int count) | |
| { | |
| await MethodAsync(count).ConfigureAwait(true); | |
| } | |
| private static async Task MethodAsync(int count) | |
| { | |
| if (count == 0) | |
| { | |
| await TaskTAsync(count); | |
| } | |
| else | |
| { | |
| await ConfiguredAsync(count - 1); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment