在Visual Studio中运行发行版并在Visual Studio外部运行发行版时,以下代码提供了不同的输出.我正在使用Visual Studio 2008并以.NET 3.5为目标.我也尝试过.NET 3.5 SP1.
当在Visual Studio外部运行时,JIT应该启动.或者(a)C#中有一些微妙的东西我缺失或者(b)JIT实际上是错误的.我怀疑JIT可能出错,但我已经没有其他可能性......
在Visual Studio中运行时输出:
0 0,
0 1,
1 0,
1 1,
Run Code Online (Sandbox Code Playgroud)
在Visual Studio外部运行发布时的输出:
0 2,
0 2,
1 2,
1 2,
Run Code Online (Sandbox Code Playgroud)
是什么原因?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
struct IntVec
{
public int x;
public int y;
}
interface IDoSomething
{
void Do(IntVec o);
}
class DoSomething : IDoSomething
{
public void Do(IntVec o)
{
Console.WriteLine(o.x.ToString() + " " + o.y.ToString()+",");
}
}
class Program
{ …Run Code Online (Sandbox Code Playgroud) 对下面的程序非常好奇(是的,在未附带调试器的情况下在发布模式下运行),第一个循环为数组的每个元素分配一个新对象,并且需要大约一秒钟才能运行.
所以我想知道哪个部分占用了最多的时间 - 对象创建或分配.所以我创建了第二个循环来测试创建对象所需的时间,第三个循环来测试分配时间,并且都在几毫秒内运行.这是怎么回事?
static class Program
{
const int Count = 10000000;
static void Main()
{
var objects = new object[Count];
var sw = new Stopwatch();
sw.Restart();
for (var i = 0; i < Count; i++)
{
objects[i] = new object();
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds); // ~800 ms
sw.Restart();
object o = null;
for (var i = 0; i < Count; i++)
{
o = new object();
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds); // ~ 40 ms
sw.Restart();
for (var i = 0; …Run Code Online (Sandbox Code Playgroud)