Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IList<RepeaterItem> items;
- var result = items
- .Select(item => item.FindControl("somecontrol"))
- .Where(ctrl => SomeCheck(ctrl))
- .AsParallel();
- var result = items
- .AsParallel()
- .Select(item => item.FindControl("somecontrol"))
- .Where(ctrl => SomeCheck(ctrl));
- using System;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading;
- class Test
- {
- static void Main()
- {
- var query = Enumerable.Range(0, 1000)
- .Select(SlowProjection)
- .Where(x => x > 10)
- .AsParallel();
- Stopwatch sw = Stopwatch.StartNew();
- int count = query.Count();
- sw.Stop();
- Console.WriteLine("Count: {0} in {1}ms", count,
- sw.ElapsedMilliseconds);
- query = Enumerable.Range(0, 1000)
- .AsParallel()
- .Select(SlowProjection)
- .Where(x => x > 10);
- sw = Stopwatch.StartNew();
- count = query.Count();
- sw.Stop();
- Console.WriteLine("Count: {0} in {1}ms", count,
- sw.ElapsedMilliseconds);
- }
- static int SlowProjection(int input)
- {
- Thread.Sleep(100);
- return input;
- }
- }
- Count: 989 in 100183ms
- Count: 989 in 13626ms
- var SlowProjection = new Func<int, int>((input) => { Thread.Sleep(100); return input; });
- var Measure = new Action<string, Func<List<int>>>((title, measure) =>
- {
- Stopwatch sw = Stopwatch.StartNew();
- var result = measure();
- sw.Stop();
- Console.Write("{0} Time: {1}, Result: ", title, sw.ElapsedMilliseconds);
- foreach (var entry in result) Console.Write(entry + " ");
- });
- Measure("Sequential", Enumerable.Range(0, 30)
- .Select(SlowProjection).Where(x => x > 10).ToList());
- Measure("Parallel", Enumerable.Range(0, 30).AsParallel()
- .Select(SlowProjection).Where(x => x > 10).ToList());
- Measure("Ordered", Enumerable.Range(0, 30).AsParallel().AsOrdered()
- .Select(SlowProjection).Where(x => x > 10).ToList());
- Sequential Time: 6699, Result: 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
- Parallel Time: 1462, Result: 12 16 22 25 29 14 17 21 24 11 15 18 23 26 13 19 20 27 28
- Ordered Time: 1357, Result: 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement