From 6e4193f4a8a6b94bf32b593730719b7bc6d56c64 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Mon, 15 Feb 2016 13:00:23 +0000 Subject: [PATCH] ValueTask --- dnx/Program.cs | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ dnx/project.json | 9 ++--- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/dnx/Program.cs b/dnx/Program.cs index fd85054..31e0330 100644 --- a/dnx/Program.cs +++ b/dnx/Program.cs @@ -50,6 +50,18 @@ private static void Run(bool output) } CleanUp(); + sw.Restart(); + var x5 = skynetValueTaskAsync(0, limit, 10).Result; + sw.Stop(); + var t5 = sw.Elapsed.TotalMilliseconds; + + if (output) + { + Console.WriteLine(x5); + Console.WriteLine($"1 Thread - ValueTask Async: {t5:0.000}ms"); + } + CleanUp(); + sw.Restart(); var x3 = skynetThreadpoolAsync(0, limit, 10).Result; sw.Stop(); @@ -62,6 +74,18 @@ private static void Run(bool output) } CleanUp(); + sw.Restart(); + var x6 = skynetThreadpoolValueTaskAsync(0, limit, 10).Result; + sw.Stop(); + var t6 = sw.Elapsed.TotalMilliseconds; + + if (output) + { + Console.WriteLine(x5); + Console.WriteLine($"Parallel - ValueTask Async: {t6:0.000}ms"); + } + CleanUp(); + sw.Restart(); var x4 = skynetParallel(0, limit, 10); sw.Stop(); @@ -73,6 +97,7 @@ private static void Run(bool output) Console.WriteLine($"Parallel Sync: {t4:0.000}ms"); } CleanUp(); + } private static long skynetSync(long num, long size, long div) @@ -142,6 +167,67 @@ static long skynetAggregator(Task children) return sumAsync; } + + private static ValueTask skynetValueTaskAsync(long num, long size, long div) + { + if (size == 1) + { + return num; + } + else + { + long subtotal = 0; + List> tasks = null; + + for (var i = 0; i < div; i++) + { + var sub_num = num + i * (size / div); + var task = skynetValueTaskAsync(sub_num, size / div, div); + if (task.IsCompleted) + { + subtotal += task.Result; + } + else + { + if (tasks == null) + { + tasks = new List>((int)div); + } + tasks.Add(task.AsTask()); + } + } + + if (tasks == null) + { + return subtotal; + } + else if (subtotal > 0) + { + tasks.Add(Task.FromResult(subtotal)); + } + return Task.WhenAll(tasks).ContinueWith(skynetAggregator); + } + } + + private static Task skynetThreadpoolValueTaskAsync(long num, long size, long div) + { + if (size == 1) + { + return Task.FromResult(num); + } + else + { + var tasks = new List>((int)div); + for (var i = 0; i < div; i++) + { + var sub_num = num + i * (size / div); + var task = Task.Run(() => skynetValueTaskAsync(sub_num, size / div, div).AsTask()); + tasks.Add(task); + } + return Task.WhenAll(tasks).ContinueWith(skynetAggregator); + } + } + private static long skynetParallel(long num, long size, long div) { if (size == 1) diff --git a/dnx/project.json b/dnx/project.json index 36eb6d7..0cb015b 100644 --- a/dnx/project.json +++ b/dnx/project.json @@ -4,10 +4,11 @@ "emitEntryPoint": true }, - "dependencies": { - "NETStandard.Library": "1.0.0-rc2-23811", - "System.Threading.Tasks.Parallel": "4.0.1-rc2-23805" - }, + "dependencies": { + "NETStandard.Library": "1.0.0-rc2-23811", + "System.Threading.Tasks.Extensions": "4.0.0-rc2-23812", + "System.Threading.Tasks.Parallel": "4.0.1-rc2-23805" + }, "frameworks": { "dnxcore50": { }