C++ 11:
#include <iostream>
#include <chrono>
auto start = std::chrono::system_clock::now();
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
Run Code Online (Sandbox Code Playgroud)
代码取自en.cppreference.com并简化.
旧答案:
GetTickCount()
在windows.h
返回时刻度(毫秒)已过去.当您的应用程序启动时,调用此函数并存储其值,然后每当您需要知道自程序启动以来经过的时间时,再次调用此方法并从起始值中减去其值.
int start = GetTickCount(); // At Program Start
int elapsed = GetTickCount() - start; // This is elapsed time since start of your program
Run Code Online (Sandbox Code Playgroud)
您不需要计时器,您可以在应用程序启动时保存时间戳time(0)
.而且每次你想要测量时间时都会这样做,你只需要init_time - current_time,你就会得到时间流逝.
归档时间: |
|
查看次数: |
2503 次 |
最近记录: |