#include <iostream>
#include <sys/time.h>
int main() {
struct timeval start, end;
gettimeofday(&start, NULL);
// Your code here
gettimeofday(&end, NULL);
long seconds = end.tv_sec - start.tv_sec;
long micros = end.tv_usec - start.tv_usec;
double elapsed = seconds + micros/1000000.0;
std::cout << "Program executed in " << elapsed << " seconds." << std::endl;
return 0;
}
#include <iostream>
#include <unistd.h>
#include <sys/time.h>
void timerCallback() {
std::cout << "Timer expired!" << std::endl;
}
int main() {
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
select(0, NULL, NULL, NULL, &timeout);
timerCallback();
return 0;
}
#include <iostream>
#include <sys/time.h>
int main() {
struct timeval start, end;
gettimeofday(&start, NULL);
// Some operations
gettimeofday(&end, NULL);
long seconds = end.tv_sec - start.tv_sec;
long micros = end.tv_usec - start.tv_usec;
double elapsed = seconds + micros/1000000.0;
std::cout << "The time difference is " << elapsed << " seconds." << std::endl;
return 0;
}
这些是一些C++中timeval的典型用法示例,可以根据具体的需求进行修改和扩展。