linux c++计算程序运行时长
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <unistd.h> using namespace std; int main() { // 1. // struct timeval start, end; // gettimeofday(&start, NULL); // system("pwd"); // gettimeofday(&end, NULL); // int timeuse = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec; // runTime = timeStart.tv_sec + (double)(timeStart.tv_usec)/1000000; // printf("\nstart time: %d \nend time: %d \nspent time: %d us\n", &start, &end, timeuse); // 2. struct timeval timeStart, timeEnd, timeSystemStart; double runTime=0, systemRunTime; gettimeofday(&timeStart, NULL ); usleep(300000); gettimeofday( &timeEnd, NULL ); runTime = (timeEnd.tv_sec - timeStart.tv_sec ) + (double)(timeEnd.tv_usec -timeStart.tv_usec)/1000000; // systemRunTime = (timeEnd.tv_sec - timeSystemStart.tv_sec ) + (double)(timeEnd.tv_usec -timeSystemStart.tv_usec)/1000000; printf("runTime is %lf s\n", runTime); return 0; }