您的位置:首页 > 移动开发 > Cocos引擎

[iuud8]cocos2dx编程中获得代码段执行时间,精确到毫秒级

2015-01-04 11:20 225 查看
在程序书写过程中,我们常常需要得到某一段代码的执行时间,来判断当前代码是否冗余,是否会消耗大量的系统资源,或者在资源加载的过程中,判断是那些资源消耗了大量的时间。

在c++中,有一个活的时间的办法,但精度只到秒:

struct timeval now;
struct tm* time;

gettimeofday(&now, NULL);

time = localtime(&now.tv_sec);

char date[32] = {0};
sprintf(date, "%d%02d%02d",time->tm_year + 1900, time->tm_mon + 1, time->tm_mday);


如果想要获取到毫秒级时间差,则
auto t1 = std::chrono::high_resolution_clock::now();

{
......
}

auto t2 = std::chrono::high_resolution_clock::now();
log(">>>>>>>>>>>time1 = %lld", std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count());

中间{ ...... }表示你要获取执行时间的代码段,最后输出时间即为该代码段的时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息