#include<iostream>
#include<stdio.h>
#include<string.h>
#include <Windows.h>
#include<time.h>
#include <graphics.h>
#include <conio.h>
#include<algorithm>
#include<stdlib.h>
#include <random>
#include <string>
#include <fstream>
#include <ddraw.h>
#include<iomanip>
#include <Psapi.h>
#include<vector>
#include<io.h>
#include <sapi.h> // 语音头文件
#include <sphelper.h> // 语音识别头文件
#include <thread> //多线程
//vs2019自定义控制台代码模板 by mao
//作者QQ1296193245
using namespace std;
#pragma warning(disable : 4996)
#pragma comment (lib,"ddraw.lib")
#pragma comment(lib,"sapi.lib")
#pragma comment(lib,"ole32.lib")
#pragma intrinsic(__rdtsc)
unsigned __int64 t1, t2, t3, t4;
double run_time; //微秒级算法时间计时器
LARGE_INTEGER time_start; //开始时间
LARGE_INTEGER time_over; //结束时间
double dqFreq; //计时器频率
LARGE_INTEGER f; //计时器频率
double tt;
long double totaltime;
inline void runstart()
{
QueryPerformanceFrequency(&f);
dqFreq = (double)f.QuadPart;
t1 = __rdtsc();
QueryPerformanceCounter(&time_start); //计时开始
}
inline void runend()
{
QueryPerformanceCounter(&time_over); //计时结束
run_time = 1000 * (time_over.QuadPart - time_start.QuadPart) / dqFreq;
t2 = __rdtsc();
t3 = t2 - t1;
}
void ShowProcessMemoryUsageInfo()
{
HANDLE handle = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
float memoryUsage_M = pmc.WorkingSetSize / (1024.0 * 1024.0);
float memoryUsage_K = pmc.WorkingSetSize / 1024.0;
std::cout << std::fixed << setprecision(0) << "程序内存使用率:" << memoryUsage_K << " KB = " << setprecision(2) << memoryUsage_M << " MB" << std::endl;
}
__int64 CompareFileTime(FILETIME time1, FILETIME time2)//转换为整数时间
{
__int64 a = static_cast<long long>(time1.dwHighDateTime) << 32 | time1.dwLowDateTime;
__int64 b = static_cast<long long>(time2.dwHighDateTime) << 32 | time2.dwLowDateTime;
return (b - a);
}
void rundisplay()
{
cout << endl;
cout << "-------------------------------------------------------" << endl;
if (t3 < 10000)
{
cout << "CPU脉冲数:" << (t3) << endl;
}
else if (t3 < 100000000 && t3 >= 10000)
{
tt = t3;
cout << "CPU脉冲数:" << (tt) / 10000 << "万" << endl;
}
else if (t3 > 100000000000)
{
t4 = t3;
t4 = t3 / 10000000;
tt = t4;
cout << "CPU脉冲数:" << (tt) / 10 << "亿" << endl;
}
else
{
t4 = t3;
t4 = t3 / 10000;
tt = t4;
cout << "CPU脉冲数:" << (tt) / 10000 << "亿" << endl;
}
if (run_time > 10000)
{
cout << "算法运行时间:" << run_time / 1000 << "秒" << endl;
}
else if (run_time < 1)
{
cout << "算法运行时间:" << run_time * 1000 << "微秒" << endl;
}
else
{
cout << "算法运行时间:" << run_time << "毫秒" << endl;
}
totaltime = clock();
cout << "程序运行时间:" << totaltime / 1000 << "秒" << endl;
if (run_time > 0.4)
{
double frequency = t3 / run_time / 1000;
cout << "CPU频率:" << frequency << "MHZ" << endl;
}
ShowProcessMemoryUsageInfo();
long lRet;
HKEY hKey;
TCHAR tchData[64];
DWORD dwSize;
lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Hardware\\Description\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey);
if (lRet == ERROR_SUCCESS)
{
dwSize = sizeof(tchData);
lRet = RegQueryValueEx(hKey, L"ProcessorNameString", NULL, NULL, (LPBYTE)tchData, &dwSize);
if (lRet == ERROR_SUCCESS)
{
cout << "CPU 名字:";
wcout << tchData << endl;
}
else
{
cout << "CPU 名字:";
wcout << L"未知" << endl;
}
}
else
{
cout << "CPU 名字:";
wcout << L"未知" << endl;
}
RegCloseKey(hKey);
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
double totalphy = statex.ullTotalPhys / 1024 / 1024;
totalphy = totalphy / 1024;
cout << "总物理内存: " << setprecision(4) << left << setw(7) << totalphy << " GB" << endl;
double totalvir = statex.ullTotalPageFile / 1024 / 1024;
totalvir = totalvir / 1024;
cout << "总虚拟内存: " << setw(7) << totalvir - totalphy << " GB" << endl;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = { 0, 0 }; //光标位置
CONSOLE_SCREEN_BUFFER_INFO csbi{};
if (GetConsoleScreenBufferInfo(hConsole, &csbi))
{
//printf("光标坐标:(%d,%d)\n", csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y);
}
HANDLE hStdout;
// 光标位置
COORD cursorPos;
// 标准输出句柄
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hEvent;
BOOL res;
FILETIME preidleTime;
FILETIME prekernelTime;
FILETIME preuserTime;
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime;
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);// 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled
double freephy = statex.ullAvailPhys / 1024 / 1024;
freephy = freephy / 1024;
double usephy = (totalphy - freephy) * 100.0 / totalphy;
cout << "内存使用率: " << setprecision(3) << usephy << "%" << endl;
double freetotal = statex.ullAvailPageFile / 1024 / 1024;
freetotal = freetotal / 1024;
cout << "总空闲内存: " << setprecision(3) << freetotal << " GB" << endl;
double freevir = (totalvir - freetotal) - (totalphy * statex.dwMemoryLoad / 100.0);
cout << "虚拟内存使用量:" << setprecision(3) << freevir << " GB" << endl;
double usevir = freevir * 100 / (totalvir - totalphy);
cout << "虚拟内存使用率:";
printf("%.2f%%", usevir);
cout << endl;
cout << "CPU利用率: " << right << setprecision(2) << setw(6) << 0.00 << "%" << endl;
cout << "CPU空闲率: " << setw(6) << 100.0<< "%" << endl;
cout << "-------------------------------------------------------" << endl;
cout << "按任意键退出程序" << endl;
while (!_kbhit())
{
GlobalMemoryStatusEx(&statex);
WaitForSingleObject(hEvent, 1000);
res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
int idle = CompareFileTime(preidleTime, idleTime);
int kernel = CompareFileTime(prekernelTime, kernelTime);
int user = CompareFileTime(preuserTime, userTime);
float cpu = (kernel + user - idle) * 100.0 / (kernel + user);
float cpuidle = (idle) * 100.0 / (kernel + user);
cursorPos.X = 0;
cursorPos.Y = csbi.dwCursorPosition.Y + 0;
SetConsoleCursorPosition(hStdout, cursorPos);
//cout << "内存使用率:" << statex.dwMemoryLoad << "%" << endl;
double freephy = statex.ullAvailPhys / 1024 / 1024;
freephy = freephy / 1024;
double usephy = (totalphy - freephy) * 100.0 / totalphy;
cout << "内存使用率: " << setprecision(3) << usephy << "%" << endl;
cursorPos.X = 0;
cursorPos.Y = csbi.dwCursorPosition.Y + 1;
SetConsoleCursorPosition(hStdout, cursorPos);
double freetotal = statex.ullAvailPageFile / 1024 / 1024;
freetotal = freetotal / 1024;
cout << "总空闲内存: " << setprecision(3) << freetotal <<" GB" << endl;
cursorPos.X = 0;
cursorPos.Y = csbi.dwCursorPosition.Y + 2;
SetConsoleCursorPosition(hStdout, cursorPos);
double freevir = (totalvir - freetotal) - (totalphy * statex.dwMemoryLoad / 100.0);
cout << "虚拟内存使用量:" << setprecision(3) << freevir <<" GB" << endl;
cursorPos.X = 0;
cursorPos.Y = csbi.dwCursorPosition.Y + 3;
SetConsoleCursorPosition(hStdout, cursorPos);
double usevir = freevir * 100 / (totalvir - total