📄 programtimecounter.cpp
字号:
#include <windows.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <iomanip>
#ifdef _UNICODE
std::basic_ostream<wchar_t>& tout = std::wcout;
std::basic_ostream<wchar_t>& terr = std::wcerr;
typedef std::basic_string<wchar_t> tstring;
#else
std::basic_ostream<char>& tout = std::cout;
std::basic_ostream<char>& terr = std::cerr;
typedef std::basic_string<char> tstring;
#endif
FILETIME& operator+=(FILETIME& ft1, FILETIME ft2)
{
((ULARGE_INTEGER&)ft1).QuadPart += ((ULARGE_INTEGER&)ft2).QuadPart;
return ft1;
}
FILETIME operator+(FILETIME ft1, FILETIME ft2)
{
FILETIME ft = ft1;
return ft += ft2;
}
FILETIME& operator-=(FILETIME& ft1, FILETIME ft2)
{
((ULARGE_INTEGER&)ft1).QuadPart -= ((ULARGE_INTEGER&)ft2).QuadPart;
return ft1;
}
FILETIME operator-(FILETIME ft1, FILETIME ft2)
{
FILETIME ft = ft1;
return ft -= ft2;
}
std::basic_ostream<TCHAR>& operator<< (std::basic_ostream<TCHAR>& os,
FILETIME ft)
{
SYSTEMTIME st;
FileTimeToSystemTime(&ft, &st);
if(st.wHour)
os << st.wHour << _TEXT("h");
os << st.wMinute << _TEXT("m") << st.wSecond;
os << _TEXT(".");
os << std::setw(3) << std::setfill(_TEXT('0')) << st.wMilliseconds;
os << _TEXT("s");
return os;
}
void report_error_code(DWORD code)
{
LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
// Handle the error.
return;
}
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
tout << (LPCTSTR)lpMsgBuf << std::endl;
// Free the buffer.
LocalFree( lpMsgBuf );
}
int _tmain(int argc, TCHAR** argv)
{
tstring exe_name = argv[0];
tstring commandline = GetCommandLine();
if(commandline[0]==_TEXT('\"'))
{
commandline.erase(0, 1);
commandline.erase(0, exe_name.length());
commandline.erase(0, 1);
}
else
{
commandline.erase(0, exe_name.length());
}
while(_istspace(commandline[0]))
commandline.erase(0, 1);
tout << exe_name << _TEXT("\n");
tout << commandline << _TEXT("\n");
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
if(CreateProcess(NULL, (LPTSTR)commandline.c_str(), NULL, NULL, FALSE, 0,
NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
FILETIME ct, et, kt, ut;
GetProcessTimes(pi.hProcess, &ct, &et, &kt, &ut);
tout << _TEXT("living time:") << _TEXT("\t") << et-ct << _TEXT("\n");
tout << _TEXT("total time:") << _TEXT("\t") << kt+ut << _TEXT("\n");
tout << _TEXT("kernel time:") << _TEXT("\t") << kt << _TEXT("\n");
tout << _TEXT("user time:") << _TEXT("\t") << ut << _TEXT("\n");
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
else
{
report_error_code(GetLastError());
return 1;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -