天天看点

【Vitis】时间戳获取函数clock()返回值为0的解决办法问题描述产生原因解决办法

问题描述

在Vitis中编写的裸机程序常常会需要计算程序运行时间,这时如果像在Windows/linux上调用time.h中的clock()函数返回值始终是0。

产生原因

在vitis中查找_sys_time.c,其中关于clock函数的注释如下所示,其中提到clock函数与操作系统紧密相关,故在裸机环境下调用该函数会始终返回0。

#include <time.h>

/*
 * clock -- It supposed to return processor time. We are not implementing
 *          this function, as timekeeping is tightly coupled with system, hence
 *          always returning 0. Users can override this with their system
 *          specific implementation.
 *
 */
__attribute__((weak)) clock_t clock(void)
{
    return (0);
}
           

解决办法

  1. 使用PL上的AXI_TIMER IP核作为定时器/计数器,这样可以做到精确计时
  2. 如果不需要精确计时,只需要精确到秒数可以使用如下代码,该代码在102开发板上运行可返回正确结果。
#include "xtime_l.h"
clock_t clock() {
	XTime tCur = 0;
	XTime_GetTime(&tCur);
	return (tCur/COUNTS_PER_SECOND);
}