1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Diagnostics;
5: using System.Threading.Tasks;
6:
7: namespace ParallelTests
8: {
9: public class Program
10: {
11: private const int COLLECTION_ITEM_COUNT = 1024;
12: private const int TPL_ITERATION_COUNT = 1000;
13:
14: public static void Main(string[] args)
15: {
16: // Prepare data collection
17: List<KeyValuePair<int, int>> randomData = PrepareData();
18: Console.WriteLine("Running SEQ test...");
19: RunSequentialTest(randomData);
20: Console.WriteLine("Running PAR test...");
21: RunParallelTest(randomData);
22: Console.WriteLine("Done.");
23: }
24:
25: private static List<KeyValuePair<int, int>> PrepareData()
26: {
27: List<KeyValuePair<int, int>> randomData = new List<KeyValuePair<int, int>>();
28: for (int i = 0; i < COLLECTION_ITEM_COUNT; i++)
29: {
30: randomData.Add(new KeyValuePair<int, int>(i, new Random(i).Next()));
31: }
32: return randomData;
33: }
34:
35: private static void DoWork(long i, List<KeyValuePair<int, int>> randomData)
36: {
37: for (int j = 0; j < TPL_ITERATION_COUNT; j++)
38: {
39: var data = from randomNumber in randomData
40: select new
41: {
42: randomNumber.Key,
43: randomNumber.Value
44: };
45: }
46: }
47:
48: #region Sequential
49: private static void RunSequentialTest(List<KeyValuePair<int, int>> randomData)
50: {
51: Stopwatch sw = new Stopwatch();
52: sw.Start();
53:
54: RunLINQSequentialTest(randomData);
55:
56: sw.Stop();
57: Console.WriteLine("PLINQ/SEQ: {0} msecs", sw.ElapsedMilliseconds);
58:
59: sw.Restart();
60:
61: RunSequentialForTest(randomData);
62:
63: sw.Stop();
64: Console.WriteLine("TPL/SEQ: {0} msecs", sw.ElapsedMilliseconds);
65: }
66:
67: private static void RunLINQSequentialTest(List<KeyValuePair<int, int>> randomData)
68: {
69: var data = from randomNumber in randomData
70: select new
71: {
72: randomNumber.Key,
73: randomNumber.Value
74: };
75: }
76:
77: private static void RunSequentialForTest(List<KeyValuePair<int, int>> randomData)
78: {
79: for (int i = 0; i < COLLECTION_ITEM_COUNT; i++)
80: {
81: DoWork(i, randomData);
82: }
83: }
84: #endregion
85:
86: #region Parallel
87: private static void RunParallelTest(List<KeyValuePair<int, int>> randomData)
88: {
89: Stopwatch sw = new Stopwatch();
90: sw.Start();
91:
92: RunLINQAsParallelTest(randomData);
93:
94: sw.Stop();
95: Console.WriteLine("PLINQ/PAR: {0} msecs", sw.ElapsedMilliseconds);
96:
97: sw.Restart();
98:
99: RunParallelForTest(randomData);
100:
101: sw.Stop();
102: Console.WriteLine("TPL/PAR: {0} msecs", sw.ElapsedMilliseconds);
103: }
104:
105: private static void RunLINQAsParallelTest(List<KeyValuePair<int, int>> randomData)
106: {
107: var data = from randomNumber in randomData.AsParallel()
108: select new
109: {
110: randomNumber.Key,
111: randomNumber.Value
112: };
113: }
114:
115: private static void RunParallelForTest(List<KeyValuePair<int, int>> randomData)
116: {
117: Parallel.For(0, COLLECTION_ITEM_COUNT, (i) => DoWork(i, randomData));
118: }
119: #endregion
120: }
121: }