Created
September 27, 2012 04:44
-
-
Save motowilliams/3792205 to your computer and use it in GitHub Desktop.
Side-By-Side comparison for TPL and Sequential out of band calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication34 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Sequential(); | |
TLP(); | |
} | |
private static void TLP() | |
{ | |
Console.WriteLine("\r\nUsing Task Parallelism"); | |
Stopwatch stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
DataService dataService = new DataService(); | |
Task<IDictionary<int, string>> data01 = Task.Factory.StartNew(() => dataService.GetData(numberOfPeople: 2)); | |
Task<IDictionary<int, string>> data02 = Task.Factory.StartNew(() => dataService.GetData(numberOfPeople: 3)); | |
Task<IDictionary<int, string>> data03 = Task.Factory.StartNew(() => dataService.GetData(numberOfPeople: 3)); | |
Task<IDictionary<int, string>> data04 = Task.Factory.StartNew(() => dataService.GetData(numberOfPeople: 4)); | |
Task<IDictionary<int, string>> data05 = Task.Factory.StartNew(() => dataService.GetData(numberOfPeople: 5)); | |
Task.WaitAll(data01, data02, data03, data04, data05); | |
var q = from d1 in data01.Result | |
join d2 in data02.Result on d1.Key equals d2.Key | |
join d3 in data03.Result on d2.Key equals d3.Key | |
join d4 in data04.Result on d2.Key equals d4.Key | |
join d5 in data04.Result on d2.Key equals d5.Key | |
select new { Name = d1.Value }; | |
Console.WriteLine(" {{some arbitrary result}} » User Count {0}", q.Count()); | |
Console.WriteLine(" ElapsedMilliseconds » {0}", stopwatch.ElapsedMilliseconds); | |
} | |
private static void Sequential() | |
{ | |
Console.WriteLine("\r\nUsing Sequential Calls"); | |
Stopwatch stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
DataService dataService = new DataService(); | |
var q = from d1 in dataService.GetData(numberOfPeople: 2) | |
join d2 in dataService.GetData(numberOfPeople: 3) on d1.Key equals d2.Key | |
join d3 in dataService.GetData(numberOfPeople: 3) on d1.Key equals d3.Key | |
join d4 in dataService.GetData(numberOfPeople: 4) on d1.Key equals d4.Key | |
join d5 in dataService.GetData(numberOfPeople: 5) on d1.Key equals d5.Key | |
select new { Name = d1.Value }; | |
Console.WriteLine(" {{some arbitrary result}} » User Count {0}", q.Count()); | |
Console.WriteLine(" ElapsedMilliseconds » {0}", stopwatch.ElapsedMilliseconds); | |
} | |
} | |
public class DataService | |
{ | |
private readonly Dictionary<int, string> _names = new Dictionary<int, string> { { 1, "John" }, { 2, "Paul" }, { 3, "George" }, { 4, "Ringo" } }; | |
private Guid _id; | |
public DataService() | |
{ | |
_id = Guid.NewGuid(); | |
} | |
public IDictionary<int, string> GetData(int numberOfPeople = 0) | |
{ | |
Console.WriteLine(" Fetching {0} users (class {1} / method instance {2})", numberOfPeople, _id, Guid.NewGuid()); | |
Thread.Sleep(numberOfPeople * 1000); | |
return _names.Take(numberOfPeople).ToDictionary(key => key.Key, value => value.Value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment