这是程序:
计时器.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
我是否省略了任何内容,或者我只是在使用过时的编译器?
更新我已经验证此解决方案适用于 OSX 10.8.5 上的库存 c++ 和 g++ 编译器和库。