C++获取当前时间和计算程序运行时间的方法 获取当前时间: #include <iostream> #include <Windows.h> using namespace std; int main() { SYSTEMTIME sys; GetLocalTime(&sys); cout<<sys.wYear<<"年"; cout<<sys.wMonth<<"月"; cout<<sys.wDay<<"日"; cout<<sys.wHour<<"时"; cout<<sys.wMinute<<"分"; cout<<sys.wSecond<<"秒"; cout<<sys.wMilliseconds<<"毫秒"; cout<<",星期"<<sys.wDayOfWeek<<endl; return 0; } 计算程序运行时间 方法一: #include <iostream> #include <time.h>//关键 using namespace std; int main() { clock_t start, finish; double totalTime; start = clock(); //需要测试运行时间的代码段放在这 finish = clock(); totalTime = (double)(finish - start); cout<<"花费"<<totalTime<<"毫秒"<<endl; return 0; } 计算程序运行时间 方法二: #include <iostream> #include <Windows.h> //关键 using namespace std; int main() { LONGLONG start, finish; LONGLONG totalTime; start = GetTickCount(); //需要测试运行时间的代码段放在这 finish = GetTickCount(); totalTime = finish - start; cout<<"花费"<<totalTime<<"毫秒"<<endl; return 0; }
时间: 2024-10-15 11:16:40