以下是该计划:
timer.cpp:
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <time.h>
int main( int argc, char** argv) {
#define BILLION 1E9
struct timespec requestStart, requestEnd;
// Calculate time taken by a request
clock_gettime(CLOCK_REALTIME, &requestStart);
//function_call(); // time this
clock_gettime(CLOCK_REALTIME, &requestEnd);
// Calculate time it took
double accum = ( requestEnd.tv_sec - requestStart.tv_sec )
+ (( requestEnd.tv_nsec - requestStart.tv_nsec ) / BILLION);
cout << accum << endl;
}
在Mac OSX 10.8.5上,我可以访问两个编译器: Apple LLVM版本4.2(clang-425.0.28),调用为c++
, gcc版本4.2 .1 (哇,那是旧的),调用为g++
。
c ++ timer.cpp:
error: use of undeclared identifier 'CLOCK_REALTIME'
clock_gettime(CLOCK_REALTIME, &requestStart);
g ++ timer.cpp:
timer.cpp: In function ‘int main(int, char**)’:
timer.cpp:15: error: ‘CLOCK_REALTIME’ was not declared in this scope
timer.cpp:15: error: ‘clock_gettime’ was not declared in this scope
我省略了什么,或者我只是简单地使用古老的编译器?
更新我已经确认this solution适用于OSX 10.8.5上的stock c ++和g ++编译器和库。