Nit*_*tin 11 .net c# clr performance
我想测量托管(.NET)线程的性能.具体来说,我需要衡量以下 -
线程使用CPU多长时间?
它被阻止多长时间(等待远程方法调用完成)?
使用System.Diagnostic.StopWatch没有帮助,因为它读取OS /硬件的高分辨率性能计时器功能,其中可能包括并行运行的其他线程和共享同一CPU所消耗的时间.
您可以使用此处描述的方法http://www.codeproject.com/KB/dotnet/ExecutionStopwatch.aspx ,它使用系统函数GetThreadTimes http://msdn.microsoft.com/en-us/library/ms683237(v=vs. 85).aspx
等待时间是总时间和执行时间之间的差值。
补充:我喜欢使用一次性类来衡量性能 - 它保持代码干净(硬编码控制台使用只是举例):
public class ThreadPerformance : IDisposable
{
private Stopwatch _totalStopwatch = new Stopwatch();
private ExecutionStopwatch _executionStopwatch = new ExecutionStopwatch();
public static ThreadPerformance Measure()
{
return new ThreadPerformance();
}
private ThreadPerformance()
{
_totalStopwatch.Start();
_executionStopwatch.Start();
}
public void Dispose()
{
_executionStopwatch.Stop();
_totalStopwatch.Stop();
Console.WriteLine("Performance mesurement for thread {0}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Waiting: {0}", _totalStopwatch.Elapsed - _executionStopwatch.Elapsed);
Console.WriteLine("CPU usage: {0}", _executionStopwatch.Elapsed);
}
}
Run Code Online (Sandbox Code Playgroud)
用法非常简单:
static void ThreadProc()
{
using (ThreadPerformance.Measure())
{
// do some calculations and waitings here
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2115 次 |
| 最近记录: |