Skip to main content
NDC { Oslo }
2019
David Wengier
Microsoft
@davidwengier
@davidwengier
- Donald Knuth
@davidwengier
- Donald Knuth
@davidwengier
We should forget about small efficiencies, say about
97% of the time: premature optimization is the root
of all evil. Yet we should not pass up our opportunities
in that critical 3%”
- Donald Knuth
@davidwengier
We should forget about small efficiencies, say about
97% of the time: premature optimization is the root
of all evil. Yet we should not pass up our opportunities
in that critical 3%”
- Donald Knuth
“Structured Programming with Go To Statements”, 1974@davidwengier
@davidwengier
@davidwengier
@davidwengier
“Don’t do that, that’s slow, and uses too much memory!”
@davidwengier
public static void Profile(Action func)
{
DateTime start = DateTime.Now;
for (int i = 0; i < 100; i++)
{
func();
}
Console.WriteLine(“Avg Time Elapsed {0} ms", (DateTime.Now - start).TotalMilliseconds / 100);
}
public static void Profile(Action func)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
func();
Stopwatch watch = new Stopwatch();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
watch.Start();
for (int i = 0; i < 100; i++)
{
func();
}
watch.Stop();
Console.WriteLine(“Avg Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds / 100);
}
https://benchmarkdotnet.org/
https://github.com/dotnet/BenchmarkDotNet
•
•
•
•
@davidwengier
•
•
•
•
•
•
@davidwengier
•
•
@davidwengier
•
•
•
•
•
•
•
•
•
0 bytes allocated 32 bytes allocated
•
•
0 bytes 20 bytes 32 bytes
•
•
•
•
public class HybridDictionary: IDictionary {
// These numbers have been carefully tested to be optimal.
// Please don't change them without doing thorough performance
// testing.
private const int CutoverPoint = 9;
• Should you always use Dictionary<K,V> instead of IDictionary<K,V>?
• Should you always use structs instead of classes?
• Should you always use for instead of foreach?
• Should you always use StringBuilder instead of concatenation?
• Should you always use traditional loops instead of Linq?
• Should you always avoid throwing exceptions?
• Should you always use Dictionary<K,V> instead of IDictionary<K,V>? No, it depends.
• Should you always use structs instead of classes? No, it depends.
• Should you always use for instead of foreach? No, it depends.
• Should you always use StringBuilder instead of concatenation? No, it depends.
• Should you always use traditional loops instead of Linq? No, it depends.
• Should you always avoid throwing exceptions? No, it depends.
• Should you always … anything? No, it probably depends.
@davidwengier