Posts

Showing posts from April, 2013

Performance impact of sorted arrays.

Image
Computer performance is filled with unexpected results. This morning on   stack overflow  I found  such a result, and decided to replicate it in C#. Take an array of random integers and sum only the big integers.  Next, sort the array and re-sum the big integers. Does sorting the array affect the time taken to compute the aggregation? Let's code it up: The results: Wow, it's 5 times faster to perform this sum over a sorted array then an unsorted array. What's going on? Branch prediction!  If you've never heard of branch prediction check out the stack overflow answer that inspired  this post. You can quickly replicate this experiment using LinqPad and MeasureIt.Net , by loading this file .

Getting Caller Information in C# without reflection (AKA: __LINE__ and __FILE__)

When writing tracing code it's very useful to know your caller. If you've used C or C++ you likely combined macro's,  __LINE__  and __FILE__ to implement this capability. In C# 4.5 there is a better way to solve this problem. By adding attributed function parameters you can instruct the compiler to have your callers supply their calling information.  The code (full source @  bitbucket ): public static void TraceMessage(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) { Console.WriteLine("message: " + message); Console.WriteLine("member name: " + memberName); Console.WriteLine("source file path: " + sourceFilePath); Console.WriteLine("source line number: " + sourceLineNumber); } st

DevPreview: Agile Principles Patterns and Practices Part2 II: Agile Design Principles

This post is in  devpreview - skip if you're not passionate about this topic, leave comments if you are.  I read lots, and often forget what I wrote about.  To help me remember, I’m going to take notes on my blog. Feel free to discuss what you've seen in the comments. Terminology Term Definition Entity The "thing" to which principles are applied. Client An entity which consumes this entity . Dependent The entity on which this entity depends to function. Summary of Principles At 10,000 feet, design principles ensure that changes to a software entity, due to bugs or requirement changes,  have minimal impact on clients.  Principle Elevator Pitch SRP: Single Responsibility Entities should have one and only one reason to change. OCP: Open Closed Entities should be closed to changes, but open to extension LSP: Liskov Substitution Der