我如何才能找到我的C程序的一个部分的执行时间? [英] How can I find the execution time of a section of my program in C?

查看:90
本文介绍了我如何才能找到我的C程序的一个部分的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方式来获得C中的code部分的执行时间,我已经尝试过了时间()和time.h中时钟(),但似乎时间()返回秒,时钟()似乎给我毫秒(或厘秒?)我想更多的东西precise虽然。有没有一种方法,我可以抢时间至少微秒precision?

I'm trying to find a way to get the execution time of a section of code in C. I've already tried both time() and clock() from time.h, but it seems that time() returns seconds and clock() seems to give me milliseconds (or centiseconds?) I would like something more precise though. Is there a way I can grab the time with at least microsecond precision?

这不仅需要能够编译Linux的。

This only needs to be able to compile on Linux.

推荐答案

您简称时钟()时间() - 您寻找的函数gettimeofday()
这将填补在 timeval结构,其中包含秒和毫秒。

You referred to clock() and time() - were you looking for gettimeofday()? That will fill in a struct timeval, which contains seconds and microseconds.

当然实际分辨率可达硬件

Of course the actual resolution is up to the hardware.

这篇关于我如何才能找到我的C程序的一个部分的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆