计时在Linux上运行的C ++程序中的函数 [英] Timing a function in a C++ program that runs on Linux

查看:131
本文介绍了计时在Linux上运行的C ++程序中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在用户空间执行所需的时间来计算我的C ++程序的函数。我尝试clock_gettime(CLOCK_PROCESS_CPUTIME_ID,& start)命令从程序内,但恐怕这是CPU时间,而不是我实际需要的用户时间。在这种情况下,时间程序名将不工作,因为我只是计时功能。任何帮助将是伟大的。
谢谢!

I am trying to time a function of my C++ program using the amount of time that it takes to execute in user space. I tried the clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start) command from inside the program but I am afraid that this is the CPU time and not the User Time that I actually need. The time "program name" will not work in this case because I am only timing a function. Any help would be great. Thanks!

推荐答案

使用 times()函数,请参阅: http://linux.die.net/man/2/times

这将为您的进程提供当前的用户和系统时间。你可以在进入和退出你的子程序时调用它,并减去。

This will give you current user and system time for your process. You can call it on entry and exit from your subroutine, and subtract.

你也可以使用像gprof这样的profiler,招致开销)。

You can also use a profiler like gprof, which will do this all automatically for you (but will incur overhead).

这篇关于计时在Linux上运行的C ++程序中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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