Skip to content

Commit

Permalink
ValueTask
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Feb 15, 2016
1 parent 61ad53c commit 6e4193f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
86 changes: 86 additions & 0 deletions dnx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -142,6 +167,67 @@ static long skynetAggregator(Task<long[]> children)
return sumAsync;
}


private static ValueTask<long> skynetValueTaskAsync(long num, long size, long div)
{
if (size == 1)
{
return num;
}
else
{
long subtotal = 0;
List<Task<long>> 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<Task<long>>((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<long> skynetThreadpoolValueTaskAsync(long num, long size, long div)
{
if (size == 1)
{
return Task.FromResult(num);
}
else
{
var tasks = new List<Task<long>>((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)
Expand Down
9 changes: 5 additions & 4 deletions dnx/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": { }
Expand Down

0 comments on commit 6e4193f

Please sign in to comment.