计算线程执行某个任务需要的时间——来自《Windows核心编程》

平时在写算法的过程中经常遇到算法运行时间的问题,用GetTickCount函数,可能是不精确的。看 《Windows核心编程》第七章时,遇到作者编写的这个功能,很有用,摘抄下来。一则分享,二则备用。
 __int64 FIleTimeToQuadWord(PFILETIME pft){
	 return(Int64ShllMod32(pft->dwHighDateTime, 32)|pft->dwLowDateTime);
 }
 
 void PerformLongOperation(){
	 PFILETIME ftKernelTimeStart, ftKernelTimeEnd;
	 PFILETIME ftUserTimeStart, ftUserTimeEnd;
	 PFILETIME ftDummy;
	 __int64 qwKernelTimeElapsed, qwUserTimeElapsed, qwTotalTimeElapsed;
	 
	 // Get starting times.
	 GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeStart, &ftKernelTimeStart);
	 
	 // Perform complex algorithm here.
	 
	 // Get ending time.
	 GetThreadTimes(GetCurrentThread(), &ftDummy, &ftDummy, &ftKernelTimeEnd, &ftKernelTimeEnd);
	 
	 // Get the elapsed kernel and user times by converting the start and end times from FILETIMEs to quad words,
	 // and then subtract the start times from the end times.
	 qwKernelTimeElapsed = FileTimeToQuadWord(&ftKernelTimeEnd) - FileTimeToQuadWord(&ftKernelTimeStart);
	 qwUTimeElapsed = FileTimeToQuadWord(&ftUserTimeEnd) - FileTimeToQuadWord(&ftUserTimeStart);
	 
	 // Get total time duration by adding the kernel and user times.
	 qwTotalTimeElapsed = qwKernelTimeElapsed + qwUserTimeElapsed;
	 
	 // The total wlapsed time is in qwTotalTimeElapsed.
	 
 }

你可能感兴趣的:(C++)