The Cost of Optimization

Imagine you were to write a query as follows:

var groups = new Dictionary<string, List<Process>>();foreach (var proc in Process.GetProcesses()){    if (proc.ProcessName.StartsWith("d"))    {        List<Process> bucket;        if (groups.TryGetValue(proc.ProcessName, out bucket))            bucket.Add(proc);        else            groups[proc.ProcessName] = new List<Process>() { proc };    }}

It’s most likely not immediately apparent to you this is carrying out a filtering operation as well as a grouping operation, let alone that the runtime would be able to see our intent from the rather complex preceding code. Also because you’ve prescribed the recipe to execute your algorithm very precisely, ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.