.Net 4比.Net 3.5慢吗? [英] Is .Net 4 slower than .Net 3.5?
问题描述
大家好,
我正在使用VS 2008和.Net 3.5测试某些计算的运行时.我以某种方式对结果感到不满意,我以为我启动了VS 2010 Express并尝试使用Parallel Extensions运气.我要做的第一件事是将代码复制到新的VS 2010项目中,然后重新运行测试.令我惊讶的是,它比.Net 3.5(66秒,38秒)要慢得多.我使用调试器外部的发布版本运行了这两个测试.这真让我感到困惑,因为我不敢相信性能下降会如此之大.
这是我的测试代码:
Hi all,
I was testing the runtime of some calculations with VS 2008 and .Net 3.5. Somehow unsatisfied with the result I thought I startup VS 2010 Express and try my luck with the Parallel Extensions. The first thing I''ve done was to copy the code to a new VS 2010 project and rerun my test. To my surprise it was by far slower than in .Net 3.5 (66 seconds, 38 seconds). I ran both tests with release builds outside the debugger. This really puzzles me as I somehow can''t believe the performance drop is so great.
Here is my test code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace SaTest
{
class Program
{
struct Structure
{
public float X;
public float Y;
}
static void Main(string[] args)
{
var l = new Structure[2000000];
var r = new Random();
for (int i = 0; i < l.Length; i++)
{
l[i].X = (float)(r.NextDouble() * 1000000);
l[i].Y = (float)(r.NextDouble() * 1000000);
}
int counter = 0;
double dist = 2000;
double distSquare = dist * dist;
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < l.Length; i++)
{
for (int j = 0; j < 2000; j++)
{
double tx = l[i].X - l[j].X;
double ty = l[i].Y - l[j].Y;
if (tx * tx + ty * ty <= distSquare)
//if (Math.Sqrt(tx * tx + ty * ty) <= dist)
counter++;
}
}
sw.Stop();
Console.WriteLine(counter);
Console.WriteLine(sw.Elapsed);
Console.ReadLine();
}
}
}
有没有人有过类似的经历?还是我的代码有些奇怪?如果有人可以测试一下并告诉我我的结果是否可重现,我将不胜感激.
Has anyone made similar experiences? Or is something weird wrong with my code? I would appreciate if anyone could test this and tell me if my results are reproducable.
推荐答案
在发布模式下使用.Net 2.0、3.5或4.0进行编译时,经过优化和没有溢出检查,我得到的计时是相同的.在所有情况下都为20.76-20.77秒.
XP SP3,Intel Core 2 Duo T7250、3GB RAM.
艾伦.
P.S.我删除了vars,以获取要为.NET 2编译的代码,并将修改后的源代码也用于3.5和4.
Hi,
I get identical timings when compiled with .Net 2.0, 3.5 or 4.0 in release mode with optimisations and no overflow checking. 20.76 - 20.77 seconds in all cases.
XP SP3, Intel Core 2 Duo T7250, 3GB RAM.
Alan.
P.S. I removed the vars to get the code to compile for .NET 2 and used the modified source for 3.5 and 4 as well.
这篇关于.Net 4比.Net 3.5慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!