我正在寻找一种方法来测量线程上的函数调用所需的cpu周期.
伪代码示例:
void HostFunction()
{
var startTick = CurrentThread.CurrentTick; //does not exist
ChildFunction();
var endTick = CurrentThread.CurrentTick; //does not exist
var childFunctionCost = endTick - startTick;
}
void ChildFunction()
{
//Do whatever...
Thread.Sleep(3000);
//Do some more...
}
Run Code Online (Sandbox Code Playgroud)
我不想使用秒表或其他时间测量,因为它将包括线程正在休眠的任何时间,我不想测量.我只想衡量实际工作.
这个测量需要在运行时工作,就像在我的伪代码中一样,因为结果用于确定是否应该允许子函数继续运行(我的实例是一个插件类型的体系结构),因此一个分析工具赢了'帮帮我
Han*_*ant 11
你可以调用QueryThreadCycleTime().查看链接以获取详细信息.
一些示例代码:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program {
static void Main(string[] args) {
ulong start, end;
start = NativeMethods.GetThreadCycles();
System.Threading.Thread.Sleep(1000);
end = NativeMethods.GetThreadCycles();
ulong cycles = end - start;
Debug.Assert(cycles < 200000);
}
static class NativeMethods {
public static ulong GetThreadCycles() {
ulong cycles;
if (!QueryThreadCycleTime(PseudoHandle, out cycles))
throw new System.ComponentModel.Win32Exception();
return cycles;
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool QueryThreadCycleTime(IntPtr hThread, out ulong cycles);
private static readonly IntPtr PseudoHandle = (IntPtr)(-2);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1856 次 |
| 最近记录: |