-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParallelAggregation.cs
34 lines (30 loc) · 1.19 KB
/
ParallelAggregation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelAggregation
{
class Program
{
public static void Main()
{
Stopwatch timer = new Stopwatch();
IEnumerable<int> nums = ParallelEnumerable.Range(0, int.MaxValue);
long sum = 0;
timer.Start();
sum = nums.AsParallel().Aggregate<int,long,long>(
() => 0, // seedFactory: first value of localTotal
(localTotal, n) => localTotal + n, // updateAccumulatorFunc:adding values to local total
(mainTotal, localTotal) => mainTotal + localTotal, // combineAccumulatorFunc:interlock add at mainTotal
finalResult => finalResult); // resultSelector:last return value, sum
timer.Stop();
//less than 9 secs
Console.WriteLine(timer.Elapsed.TotalMilliseconds);
Console.WriteLine(sum);
}
}
}