BOOL GetProcessTimes(
HANDLE hProcess, // specifies the process of interest
LPFILETIME lpCreationTime, // when the process was created
LPFILETIME lpExitTime, // when the process exited
LPFILETIME lpKernelTime, // time the process has spent in kernel
// mode
LPFILETIME lpUserTime // time the process has spent in user mode
);
BOOL GetThreadTimes(
HANDLE hThread, // specifies the thread of interest
LPFILETIME lpCreationTime, // when the thread was created
LPFILETIME lpExitTime, // when the thread was destroyed
LPFILETIME lpKernelTime, // time the thread has spent in kernel
// mode
LPFILETIME lpUserTime // time the thread has spent in user mode
);
Windows NT: Requires version 3.5 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
HANDLE hProcess, // specifies the process of interest
LPFILETIME lpCreationTime, // when the process was created
LPFILETIME lpExitTime, // when the process exited
LPFILETIME lpKernelTime, // time the process has spent in kernel
// mode
LPFILETIME lpUserTime // time the process has spent in user mode
);
BOOL GetThreadTimes(
HANDLE hThread, // specifies the thread of interest
LPFILETIME lpCreationTime, // when the thread was created
LPFILETIME lpExitTime, // when the thread was destroyed
LPFILETIME lpKernelTime, // time the thread has spent in kernel
// mode
LPFILETIME lpUserTime // time the thread has spent in user mode
);
Windows NT: Requires version 3.5 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
Win32 API提供两个与系统计数有关的函数,QueryPerformanceFrequency()用来取得计数的频率。另一个是QueryPerformanceCounter(),它可以返回计数器的计数值,要计算程序的耗时,只需要取得开始和结束时的计数差除以计数频率就得到时间。可是你需要知道不同芯片的计数频率。
(对于PII和PPro,计数频率和时钟频率相同。Pentium133的计数频率是1.193MHz....)
在操作执行完毕后再用GetTickCount得到结束时间.
两者相减就是所用的时间,,,
DWORD dwStartTime = ::GetTickCount();
// 操作
...
DWORD dwCount = ::GetTickCount() - dwStartTime;
int nMMSenconds = dwCount / 1000;
int nSenconds = dwCount / 1000;
int nMinutes = nSenconds / 60;
int nHours = nMinutes / 60;
nMMSenconds = nMMSenconds % 1000;
nSenconds = nSenconds % 60;
nMinutes = nMinutes % 60;
TRACE("干这个活儿共花费:\t%d小时%d分钟%d秒%d毫秒\n", nHours, nMinutes, nSenconds, nMMSenconds);
你如果嫌它不精确,那那........
100ns不可能,除非自已作定时器的驱动程序,或者象panda_w(好想睡啊!)提的方法,不断地查现在的counter数,直到到了100ns,不过你的程序可干不了别的了,单独作个线程也可以。