【发布时间】:2012-12-28 05:05:10
【问题描述】:
我在一个 while 循环中运行这段代码大约 10000 次,我注意到 timeSpent 大约是 4,除了第一次,大约是 500,为什么?
Stopwatch s;
long price;
count = 10000;
while (count!=0)
{
s = Stopwatch.StartNew();
price = classA.Method(..some inputs...); //this method do some iterations to return back a long given some inputs
s.Stop();
timeSpent = s.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
s.Reset();
/*write the price, timeSpent and inputs into a .txt file*/
count--;
}
【问题讨论】:
-
什么是 /* 函数调用 */?其中一个可能正在初始化某些东西。
-
您不应该在在循环之外启动秒表吗?
-
即使它正在初始化一些东西,当它再次被调用时它会再次初始化不是吗?
-
我怀疑你省略的函数调用是第一次被击中,你会看到使用 JIT 语言的成本。
-
使用 Stopwatch.ElapsedMilliseconds 比使用 Ticks 自己计算要容易得多
标签: c# .net performance stopwatch