今天遇到一个问题。为什么一个程序在vs2005中,按F5运行,和直接运行exe文件结果不一样?
写了一个类,目的是测试一段代码所用的CPU时间。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class
Timing
{
TimeSpan sTime;
TimeSpan eTime;
public TimeSpan EnderingTime
{
get { return eTime; }
}
public void TimeStart()
{
GC.Collect();
GC.WaitForPendingFinalizers();
sTime = Process.GetCurrentProcess().Threads[ 0 ].UserProcessorTime;
}
public void TimeStop()
{
eTime = Process.GetCurrentProcess().Threads[ 0 ].UserProcessorTime.Subtract(sTime);
}
}
{
TimeSpan sTime;
TimeSpan eTime;
public TimeSpan EnderingTime
{
get { return eTime; }
}
public void TimeStart()
{
GC.Collect();
GC.WaitForPendingFinalizers();
sTime = Process.GetCurrentProcess().Threads[ 0 ].UserProcessorTime;
}
public void TimeStop()
{
eTime = Process.GetCurrentProcess().Threads[ 0 ].UserProcessorTime.Subtract(sTime);
}
}
测试的时候是这样的
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class
Program
{
static void Main( string [] args)
{
Timing timeTest = new Timing();
timeTest.TimeStart();
。。。
一个函数操作
DisplayNubs(nbs);
。。。
timeTest.TimeStop();
Console.WriteLine( " it took {0} seconds. " , timeTest.EnderingTime.TotalSeconds);
Console.ReadLine();
}
}
{
static void Main( string [] args)
{
Timing timeTest = new Timing();
timeTest.TimeStart();
。。。
一个函数操作
DisplayNubs(nbs);
。。。
timeTest.TimeStop();
Console.WriteLine( " it took {0} seconds. " , timeTest.EnderingTime.TotalSeconds);
Console.ReadLine();
}
}
在vs2005中编译运行时,结过一只是“it took 0 seconds”。而直接双击exe文件运行时,却能显示正常的运行时间。为什么?