Why .net 2.0 is faster than 4.0

I want to check the performance of my computers so I ran this code

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DateTime dt = DateTime.Now; Int64 i = 0; do { i++; } while (dt.AddSeconds(10) >= DateTime.Now); this.textBox1.Text = i.ToString("n0"); } } 

when I select .NET Framework 2.0, I get a result of 33 million but when I select 4.0, I get only 9 million

+4
source share
1 answer

Actually not anwer, but too long for a comment ... The problem with the code in the question seems to be related to the performance difference between .NET 2 and 4. In particular, I see DateTime.Now much slower in .NET 4 than in version 2.0.

The result of the test console application below (after several attempts) is about 300, 310 milliseconds for .net 2 and 1050, 1090 milliseconds on .NET 4.

Posting code so that others can try other configuration platforms. I am in Windows 7-64 bits compiling in release mode.

 using System; using System.Diagnostics; class Program { public static void Main(string[] args) { const int m = 1000000; DateTime d; Stopwatch s1 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { d=DateTime.Now; } s1.Stop(); Stopwatch s2 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { DateTime.Now.AddSeconds(10); } s2.Stop(); Console.WriteLine("{0}, {1}",s1.ElapsedMilliseconds,s2.ElapsedMilliseconds); Console.Read(); } } 
+1
source

Source: https://habr.com/ru/post/1434227/


All Articles