我需要找到一个瓶颈,并且需要准确地测量时间.
以下代码段是衡量性能的最佳方法吗?
DateTime startTime = DateTime.Now;
// Some execution process
DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime.Subtract(startTime);
是否可以Environment.TickCount用来计算时间跨度?
int start = Environment.TickCount;
// Do stuff
int duration = Environment.TickCount - start;
Console.WriteLine("That took " + duration " ms");
因为TickCount已签名并将在25天后翻转(所有32位需要50天,但如果你想对数学有任何意义,你必须废弃已签名的位),看起来它太冒险了.
我正在使用DateTime.Now.这是最好的方法吗?
DateTime start = DateTime.Now;
// Do stuff
TimeSpan duration = DateTime.Now - start;
Console.WriteLine("That took " + duration.TotalMilliseconds + " ms");
我想对我的代码进行一些基本的分析,但发现C#中的DateTime.Now只有大约16毫秒的分辨率.必须有更好的时间保留我尚未找到的构造.
我需要在Windows中对.NET程序(C#)做一些性能基准测试,但我还没有在Windows世界中做过很多基准测试.我已经研究过使用带有自定义计数器的Windows 2000/XP性能监视器,但我认为这不是我想要的.
在Windows XP中是否有适用于此的良好系统设施,或者我是否需要使用System.Diagnostics.Stopwatch [编辑]并编写文本日志以进行手动解释,或者还有其他什么?
编辑:除此之外还有什么System.Diagnostics.Stopwatch?
我想知道是否存在用于精确时间测量的".net集成"解决方案(如功能的执行时间)?目前我正在使用Kernel32中的PerformanceCounters.
    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceCounter(
        out long lpPerformanceCount);
    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency(
        out long lpFrequency);
我正在玩C#集合,我决定编写一个快速测试来衡量不同集合的性能.
我的性能测试是这样的:
int numOps= (put number here);
long start, end, numTicks1, numTicks2;
float ratio;
start = DateTime.Now.Ticks;
for(int i = 0; i < numOps; i++)
{
  //add two elements to collection #1
  //remove one element from collection #1
}
end = DateTime.Now.Ticks;
numTicks1 = end - start;
start = DateTime.Now.Ticks;
for(int i = 0; i < numOps; i++)
{
  //add two elements to collection #2
  //remove one element from collection #2
}
end = DateTime.Now.Ticks;
numTicks2 = end - start; ….net ×5
c# ×5
performance ×3
datetime ×2
timer ×2
benchmarking ×1
comparison ×1
hashtable ×1
list ×1
profiling ×1
timespan ×1
windows ×1