64,636
社区成员
发帖
与我相关
我的任务
分享
STARTUPINFO si;
PROCESS_INFORMATION pi;
FILETIME ct;
FILETIME et;
FILETIME kt;
FILETIME ut;
__int64 tm=0;
SYSTEMTIME st;
if(2 > argc)
return 1;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
argv[1], // Command line. 命令行的参数
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 1;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
GetProcessTimes(pi.hProcess,&ct,&et,&kt,&ut);
FileTimeToSystemTime(&ct,&st);
printf("Creation Time: %04d-%02d-%02d %02d:%02d:%02d.%03d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
FileTimeToSystemTime(&et,&st);
printf("Exit Time: %04d-%02d-%02d %02d:%02d:%02d.%03d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
tm = (((__int64)kt.dwHighDateTime)<<32) + (__int64)kt.dwLowDateTime;
printf("Kernel Time : %llu ms\n", tm/10000);
tm = (((__int64)ut.dwHighDateTime)<<32) + (__int64)ut.dwLowDateTime;
printf("User Time : %llu ms\n",tm/10000);
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
FILETIME ct;
FILETIME et;
FILETIME kt;
FILETIME ut;
__int64 ktStart=0;
__int64 utStart=0;
__int64 ktStop=0;
__int64 utStop=0;
LARGE_INTEGER ss,ee,fq;
HANDLE cur;
int i,j,dump;
cur = GetCurrentThread();
QueryPerformanceCounter(&ss); //QueryPerformanceCounter的单位应该是cpu的时间周期时间,1/cpu频率
GetThreadTimes(cur,&ct,&et,&kt,&ut); // 起始时的内核时间和用户时间。不过精度也就10ms级
ktStart = (((__int64)kt.dwHighDateTime)<<32) + (__int64)kt.dwLowDateTime;
utStart = (((__int64)ut.dwHighDateTime)<<32) + (__int64)ut.dwLowDateTime;
// put your code here, such as:
Sleep(10000); //sleep消耗的不是程序所用的时间。scanf函数等待的时间等也一样不是程序消耗的时间
for(i=0;i<10000;i++)
{
dump = 0;
for(j=0;j<10000;j++)
{
dump += j*j;
}
}
// finish
GetThreadTimes(cur,&ct,&et,&kt,&ut); // 结束时的内核时间和用户时间
ktStop = (((__int64)kt.dwHighDateTime)<<32) + (__int64)kt.dwLowDateTime;
utStop = (((__int64)ut.dwHighDateTime)<<32) + (__int64)ut.dwLowDateTime;
printf("Kernel Time: %llu ms\n", (ktStop - ktStart)/10000); //FILETIME的单位是100ns,除10000变ms
printf("User Time: %llu ms\n", (utStop - utStart)/10000);
QueryPerformanceCounter(&ee);
QueryPerformanceFrequency(&fq);
// QueryPerformanceCounter高精度的,但记得并不是代码实际消耗的时间。会把sleep的那10秒也计入。
printf("Time: %llu us\n", (ee.QuadPart-ss.QuadPart)/(fq.QuadPart/1000000));