#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的典型用法示例,可以根據具體的需求進行修改和擴展。