获取进程启动时间
2012-07-16 17:22
302 查看
我们有时候通过测试一个程序的启动时间来评估程序的性能,那么该怎么办呢?现在提出两种办法,各有优劣,请酌情采纳。
1、通过一个辅助程序调用CreateProcess
启动被测程序,在 CreateProcess命令行参数中传递当前TickCount,在被测程序的main 函数开头获取当前TickCount,并从命令行参数中解出启动进程的TickCount,执行减法即可;
2、通过Windows API实现,需要用到的API 有:GetProcessTimes、FileTimeToSystemTime、GetSystemTime、GetCurrentProcess 等,不清楚的API请查看MSDN
或者查看官方文档。
方法1:
方法二:
在第二种方法中,我们通过
GetProcessTimes 取得进程启动时间,然后通过函数 FileTimeToSystemTime将FILETIME转换成
SYSTEMTIME,然后得到与当前系统时间的差值即可。
需要注意的是,第一种方法得到的是调用
CreateProcess 之前的那个时间戳,所以精度上可能不够精确(天晓得调用
CreateProcess 函数中做了什么);第二种方法在时间转换上较麻烦,可能还要在
dwBeginTime 和 dwEndTime 中加上Day、Month、Year。
1、通过一个辅助程序调用CreateProcess
启动被测程序,在 CreateProcess命令行参数中传递当前TickCount,在被测程序的main 函数开头获取当前TickCount,并从命令行参数中解出启动进程的TickCount,执行减法即可;
2、通过Windows API实现,需要用到的API 有:GetProcessTimes、FileTimeToSystemTime、GetSystemTime、GetCurrentProcess 等,不清楚的API请查看MSDN
或者查看官方文档。
方法1:
// helper exe. // // ... DWORD dwStartTime = ::GetTickCount(); char szBuffer[20] = {0}; sprintf_s(szBuffer, 20, "xxx.exe %u", dwStartTime); STARTUPINFO sa = {sizeof(STARTUPINFO)}; PROCESS_INFORMATION pi; ::CreateProcess( "", szBuffer, NULL, NULL, NULL, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi ); // xxx.exe // #include <iostream> using namespace std; int main(int argc, char* argv[]) { DWORD dwStartFinishTime = ::GetTickCount(); if (argc >= 2) { DWORD dwStartBeginTime = atoi(argv[1]); cout<<"Start Used Time: "<<dwStartFinishTime - dwStartBeginTime<<" ms"<<endl; } // ... return 0; }
方法二:
#include <iostream> #include <Windows.h> using namespace std; int main() { SYSTEMTIME endTime; ::GetSystemTime(&endTime); FILETIME loadStartTime, exitTime, kernelTime, userTime; HANDLE hProcess = ::GetCurrentProcess(); ::GetProcessTimes(hProcess, &loadStartTime, &exitTime, &kernelTime, &userTime); SYSTEMTIME beginTime; ::FileTimeToSystemTime(&loadStartTime, &beginTime); DWORD dwBeginTime = beginTime.wHour * 60 * 60 * 1000 + beginTime.wMinute * 60 * 1000 + beginTime.wSecond * 1000 + beginTime.wMilliseconds; DWORD dwEndTime = endTime.wHour * 60 * 60 * 1000 + endTime.wMinute * 60 * 1000 + endTime.wSecond * 1000 + endTime.wMilliseconds; cout<<"Start Used Time: "<<dwEndTime - dwBeginTime<<" ms"<<endl; return 0; }
在第二种方法中,我们通过
GetProcessTimes 取得进程启动时间,然后通过函数 FileTimeToSystemTime将FILETIME转换成
SYSTEMTIME,然后得到与当前系统时间的差值即可。
需要注意的是,第一种方法得到的是调用
CreateProcess 之前的那个时间戳,所以精度上可能不够精确(天晓得调用
CreateProcess 函数中做了什么);第二种方法在时间转换上较麻烦,可能还要在
dwBeginTime 和 dwEndTime 中加上Day、Month、Year。
相关文章推荐
- C#获取系统服务+进程+启动时间
- VC获取进程启动和结束时间
- VC获取进程启动和结束时间
- VC获取进程启动和结束时间
- 获取进程运行的时间源代码
- C++获取进程启动参数
- 基于visual c++之windows核心编程代码分析(52)使用WMI 获取进程启动参数
- Python写自动化之启动进程并获取进程输出
- Android获取开机启动总时间
- 查看进程启动时间
- 【转】获取Activity启动时间的三种方式
- Android系统启动时间(不是系统当前时间)的获取
- linux 下 查看进程的启动时间和运行时间
- 查看开机时间&查看进程启动时间
- 获取进程的开始、结束时间
- Android中获取CPU负载和进程cpu时间
- linux获取进程执行时间方法
- linux查看进程的启动时间
- Linux查看进程启动时间