Skip to content

Instantly share code, notes, and snippets.

@SoulFireMage
Created October 17, 2014 08:21
Show Gist options
  • Save SoulFireMage/bc6cdab0fba4f0659e01 to your computer and use it in GitHub Desktop.
Save SoulFireMage/bc6cdab0fba4f0659e01 to your computer and use it in GitHub Desktop.
Foreach and Dictionary initialising
//I had a need to test a few combinations of AsParallel and OrderBy whilst learning C#
//Here I wanted to try initialising a dictionary inline - not keen on code repetition.
//Outcome was for this dataset, AsParallel didn't help but worsened performance.
//Note, I tried more combinations than this.
private void timedResult(IEnumerable<Data> data)
{
var NoOrderNoParallel = data.Where(g => g.CODE == "BXF04A").Select(g => g);
var OrderedOnly = data.OrderBy(c => c.CODE).Where(g => g.CODE == "BXF04A").Select(g => g);
var ParallelOnly = data.AsParallel().Where(g => g.CODE == "BXF04A").Select(g => g);
var ParallelAndOrdered = data.AsParallel().OrderBy(c => c.CODE).Where(g => g.CODE == "BXF04A").Select(g => g);
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
//Create a dictionary, fill it and iterate in one line
foreach ( var testList in new Dictionary<IEnumerable<Data>, string>{{NoOrderNoParallel,"No Order, No Parallel"},
{OrderedOnly,"Ordered on Postcode"},
{ParallelOnly,"Parallel"},
{ParallelAndOrdered,"ParallelAndOrdered"}} )
{
timer.Start();
//Create evaluation
var test = testList.Key.ToList();
timer.Stop();
Console.WriteLine("{1} time elapsed {0}", timer.ElapsedMilliseconds, testList.Value);
timer.Reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment