使用select和chrono对程序进行cpu计时



#include <chrono>

#include <iostream>

using std::chrono::high_resolution_clock;
using std::chrono::microseconds;
using std::chrono::duration_cast;
using namespace std;

void proc_example()
{
for (int i=0; i<10000000; i++);
}

int main(int argc, char const *argv[])
{
high_resolution_clock::time_point t = chrono::high_resolution_clock::now();
const long busyTime =100;
long startTime =0 ;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000 * busyTime;
int c = 1;
int d = 0;
while(true)
{
// cout<<"running"<<endl;
t = high_resolution_clock::now();
proc_example();
tv.tv_usec = 1000000 - duration_cast<microseconds>(high_resolution_clock::now() - t).count();
select(0, NULL, NULL, NULL, &tv);
c++;
if(c == 60)
{
d++;
printf("%d min\n", d);
c = 0;
}
}
return 0;
}