
The .NET Framework has become faster. Version 4.0 executes programs faster than the 3.5 SP1. Microsoft has published information about how size of some deployments has decreased with the new version. I investigate runtime performance with a single C# program in both versions of the Framework.
To test both versions of the .NET Framework, I developed a program that uses the Dictionary collection, a for-loop, some branches, multiplications, and two Math methods. In the Dictionary lookups, unsafe code is also executed as part of the hash code computation. Please note that there may have been some changes in the Dictionary collection and Math methods that were not tested separately.
Math.Sqrt Method Math.Pow MethodThis C# program benchmarks the performance of .NET 3.5 SP1 and .NET 4.0.
Program that tests performance across frameworks [C#]
using System;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
static Dictionary<string, bool> _dict = new Dictionary<string, bool>();
const int _max = 50000000;
static void Main()
{
_dict.Add("cat", true);
_dict.Add("dog", false);
Console.WriteLine(Method(1000));
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Method(i);
}
s1.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
_max).ToString("0.00 ns"));
Console.Read();
}
static int Method(int i)
{
int value = 0;
// Dictionary lookup.
bool a;
if (_dict.TryGetValue("cat", out a))
{
value++;
}
bool b;
if (_dict.TryGetValue("nope", out b))
{
value++;
}
// Multiply by two ten times.
for (int y = 0; y < 10; y++)
{
// Use parameter.
if (y != i)
{
value *= 2;
}
}
// Take square root.
int result = (int)Math.Sqrt(value);
// Cube it.
result = (int)Math.Pow(result, 3);
return result;
}
}
When run on .NET 3.5 SP1
160.47
170.07
162.52
175.43
161.66
Average: 166.03
When run on .NET 4.0
158.19
159.69
163.40
153.16
159.94
Average: 158.88
Methodology. Here, let me explain some of my testing methodology. I compiled this program in Visual Studio 2008 and also Visual Studio 2010 in Release mode. Then, I opened the executable directly and recorded the benchmark result.
Results. I was pleasantly surprised by the results of this experiment. The average time for .NET 3.5 SP1 to execute this method was 166 nanoseconds, while .NET 4.0 required only 158 nanoseconds. Therefore, in this benchmark .NET 4.0 performed about 5% faster.
Correction: The average was incorrect in the first version of the article, which meant the performance advantage of .NET 4.0 was overstated as well. Sylvain Guéant wrote in to report the error.

Equivalent programs compiled and executed using the .NET 4.0 Framework may execute faster than those based on .NET 3.5 SP1. Therefore, your software will probably get an easy performance boost if you move it to the .NET 4.0 Framework. Because C# is generally backwards compatible, this move should be fairly simple.
.NET Framework Info