#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(" start time: %d end time: %d spent time: %d us ", &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 ", runTime); return 0; }