Lib C里面提供了一个clock()函数(它被<time.h>头文件收录)可以用于计算程序某个环节运行时间.
#include <time.h> #include <iostream> using namespace std; int main() { clock_t start,end; start=clock(); int m=0; for(int i=0;i<1e3;i++) { for(int j=0;j<1e3;j++) { m++; } } end=clock(); double n=(double)(end-start)/CLOCKS_PER_SEC; std::cout<<n<<endl;//0.002865 cout<<n<<endl;//0.002865 cout<<m<<endl;//1000000 }
ARMCC也提供了这个函数, 但是需要自己时现实中的初始化函数.
这个初始化函数只需要实现, 不需要程序员调用, LibC的初始化函数会自动调用这个函数.
/* * This will be called during startup if it's defined, in order to * allow a user reimplementation of getenv() to initialise itself. */ extern void _getenv_init(void);
这里分享下, 如何实现这个功能, 这样很多调用clock()函数获取时间的代码可以方便移植.
只有注册用户才能在此添加评论。 如果您已经注册,请登录。 如果您还没有注册,请注册并登录。