公告

大中华汽车电子生态圈社区并入开发者社区- 更多资讯点击此

Tip / 登入 to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

XMC实验分享之九十八: LibC库的clock函数实现

XMC实验分享之九十八: LibC库的clock函数实现

User12775
Level 5
Level 5
First solution authored First like received

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()函数获取时间的代码可以方便移植.

0 点赞
696 次查看
5 评论