double t = (double)getTickCount();
// do something ...
t = ((double)getTickCount() - t)/getTickFrequency();
cout << "run time: " << time << "s" << endl;
double t = (double)getTickCount();
//The function returns the number of ticks per second. That is, the following code computes the execution time in seconds:
使用C++ STL的接口:
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
using namespace std::chrono_literals;
std::cout << "Hello waiter\n" << std::flush;
auto start = std::chrono::high_resolution_clock::now();
// do something .....
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed = end-start;
std::cout << "Waited " << elapsed.count() << " ms\n";
}
计算某一函数接口运行时间
参考: