方法1
#include <ctime>
clock_t start = clock();
// do something ...
clock_t end = clock();
cout<<"花费了:"<<(double)(end-start)/CLOCK_PER_SEC<<"秒"<<endl;
方法2
using namespace chrono;
auto start = system_clock::now();
// do something ...
auto end = system_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout<<"花费了:"<<duration.count() * 1.0 / 1000<<"秒"<<endl;
方法3
#include <sys/time.h>
struct timeval start, end;
gettimeofday(&start, NULL);
// do something ...
gettimeofday(&end, NULL);
auto usedTime = (end.tv_sec - start.tv_sec) * 1000.0 + (end.tv_usec - start.tv_usec) / 1000.0;
cout<<"花费了"<<usedTime<<"ms"<<endl;