martedì 13 aprile 2010

Benchmark of the .Net Framework 4.0

I was interestend in the performance differences between the various .Net framework versions so I made a bunch of simple applications that does the following:
1) a loop that 500000000x adds a random number to an Int64
2) a loop that inserts 30000x random numbers in a listbox without BeginUpdate
3) a loop taht inserts 30000x random numbers in a listbox with BeginUpdate

I tested the Windows Forms using the .Net Framework 2.0, 3.5, 4.0 and the WPF using the .Net Framework 4.0 and these are the results:

.Net Framework 2.0:
Sum loop: 5968ms
Insert without BeginUpdate: 4393ms
Insert with BeginUpdate: 154ms

.Net Framework 3.5:
Sum loop: 5999ms
Insert without BeginUpdate: 4435ms
Insert with BeginUpdate: 159ms

.Net Framework 4.0:
Sum loop: 6024ms
Insert without BeginUpdate: 4634ms
Insert with BeginUpdate: 159ms

.Net Framework 4.0 WPF:
Sum loop: 6953ms
Insert: 155ms

The difference between the various versions of the .Net framework is really marginal, there is a trend that shows that every new version of the framework adds a bit of overhead.

The strange thing is that the loop code has nothing to do with the user interface. But when using the WPF is up to a second slower! I made the test many times and the result is always the same, the WPF code is slower than the same code in Windows Forms.

The UI responsiveness is nearly the same in this benchmark.
When resizing a form with anchored controls, bot tecnologies leaves a bit of lag on redraw. But the WPF implementation is better, feels more smooth. But the difference is not substantial.

In this benchmark the only substantial UI difference is that scrolling a big ListBox in WPF is laggy compared to the WF experience. In other words, when you drag the listbox's scrollbar in WPF it lags behind your mouse.

Here you can download the benchmark VS2010 project.