Linux计算程序耗时

写了一个linux下统计程序耗时的代码,发现linux下和windows下还有些不同,写篇博客记录下。

(1)    time.h和sys/time.h的区别

time.h是ISO C99标准日期头文件,而sys/time.h是linux系统日期时间头文件。

(2)    linux下的sleep()参数是秒为单位,而windows下是以毫秒为单位。

(3)    linux下的clock()返回微秒us,而windows下返回毫秒ms

(4)    使用clock(),linux和windows平台下要注意了,clock()是计算cpu的时间。

linux下的一个统计程序耗时:

#include <stdio.h>

#include <sys/time.h>

#include <time.h>

 

int main(int argc,char** argv)

{

struct timevaltvpre,tvafter;

gettimeofday(&tvpre,NULL);

sleep(3);

gettimeofday(&tvafter,NULL);

printf("lasttime:%d ms\n",(int)((tvafter.tv_sec-tvpre.tv_sec)*1000+(tvafter.tv_usec- tvpre.tv_usec)/1000));


return 0;

}

Linux 环境下,计算应用程序或任务的执行时间是性能分析中的常见需求。可以通过多种方法实现这一目标,包括使用内建命令、系统调用以及性能分析工具。 ### 使用 `time` 命令 最简单的方法之一是使用 `time` 命令来测量程序的整体执行时间。该命令可以显示程序运行的实时时间、用户态时间和内核态时间: ```bash $ time ./my_application ``` 输出示例: ``` real 0m1.234s user 0m0.567s sys 0m0.123s ``` - `real` 表示从开始到结束的总时间(墙上时钟时间)。 - `user` 表示用户空间代码消耗的 CPU 时间。 - `sys` 表示内核空间代码消耗的 CPU 时间。 这种方法适用于粗略估算整个程序的执行时间,但无法提供更细粒度的时间测量[^2]。 ### 使用 C/C++ 中的 `clock()` 函数 如果需要在程序内部进行更精确的时间测量,可以在代码中插入计时逻辑。例如,在 C/C++ 中可以使用 `<time.h>` 头文件中的 `clock()` 函数: ```c #include <stdio.h> #include <time.h> int main() { clock_t start, end; double cpu_time_used; start = clock(); // 执行任务 for (int i = 0; i < 1000000; i++) { // 模拟工作 } end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf("Time taken: %f seconds\n", cpu_time_used); return 0; } ``` 这种方式适用于测量函数级或模块级的执行时间,但其精度受限于系统时钟滴答数(通常为 1ms)[^4]。 ### 使用 `gettimeofday()` 或 `clock_gettime()` 进行高精度计时 为了获得更高的时间分辨率(微秒甚至纳秒级别),可以使用 `gettimeofday()` 或 POSIX 的 `clock_gettime()`: ```c #include <stdio.h> #include <sys/time.h> int main() { struct timeval start, end; long mtime, seconds, useconds; gettimeofday(&start, NULL); // 执行任务 for (int i = 0; i < 1000000; i++) { // 模拟工作 } gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = (seconds * 1000000 + useconds) / 1000; printf("Elapsed time: %ld milliseconds\n", mtime); return 0; } ``` 对于更高精度和线程安全的支持,建议使用 `clock_gettime()` 并指定 `CLOCK_MONOTONIC` 时钟源以避免系统时间调整的影响: ```c #include <time.h> #include <stdio.h> int main() { struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); // 执行任务 for (int i = 0; i < 1000000; i++) { // 模拟工作 } clock_gettime(CLOCK_MONOTONIC, &end); double time_taken = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; printf("Time taken: %.9f seconds\n", time_taken); return 0; } ``` 此方法适合对关键路径进行精确计时,并且能够有效反映任务在真实时间上的消耗[^3]。 ### 使用 Perf 工具进行性能剖析 Linux 提供了强大的性能分析工具 `perf`,它不仅可以测量程序的整体执行时间,还能深入分析各个函数调用的耗时情况。例如: ```bash $ perf stat ./my_application ``` 输出示例: ``` Performance counter stats for './my_application': 1,234,567,890 cycles # 3.456 GHz 1,000,123 instructions # 0.81 insn per cycle 123,456 branches # 34.566 M/sec 12,345 branch-misses # 9.99% of all branches 0.357890321 seconds time elapsed ``` 此外,`perf record` 和 `perf report` 可用于生成火焰图(Flame Graph),帮助识别热点函数[^3]。 ### 使用 Valgrind 进行内存与性能分析 Valgrind 不仅是一个内存调试工具,还提供了 `callgrind` 子工具用于性能分析。它可以统计每个函数的调用次数和执行时间: ```bash $ valgrind --tool=callgrind ./my_application ``` 之后使用 `kcachegrind` 或 `qcacheGrind` 查看详细的性能报告,获取函数级别的执行时间信息[^4]。 ### 总结 不同的场景适合不同的计时方法: - **粗略整体计时**:使用 `time` 命令。 - **代码内部计时**:使用 `clock()`、`gettimeofday()` 或 `clock_gettime()`。 - **高精度剖析**:使用 `perf` 工具链。 - **详细函数级分析**:使用 Valgrind 的 `callgrind` 工具。 通过结合这些方法,开发者可以在不同层次上准确评估任务执行时间并优化性能瓶颈。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值