配置文件程序在Linux上的速度
我有一个程序的几个变体,我想在性能上进行比较。两者基本上执行相同的任务。
一个在C和内存中完成。另一个调用外部实用程序并执行文件IO。
我如何可靠地比较它们?
1)使用"时间"获取" CPU上的时间"有助于调用system()和执行IO的第二种变体。即使我将"系统"时间添加到"用户"时间,也不会算作在wait()上花费的时间。
2)我不能只为它们计时,因为它们可以在服务器上运行,并且可以随时从CPU中退出。平均进行1000多次实验是一个软选择,因为我不知道服务器的使用方式(它是群集中的VM),因此有点复杂。
3)探查器无济于事,因为它们会让我花时间在代码上,这再次偏爱使用system()的版本
我需要累加这些程序消耗的所有CPU时间,包括用户,内核,IO和子进程的时间。
我以为这是一个普遍的问题,但似乎仍未找到解决方案。
(已解决了times(),请参见下文。谢谢大家)
解决方案
据我了解,在bash命令行上键入" time myapplication"不是我们想要的。
如果需要准确性,则必须使用探查器。
尝试使用Oprofile或者Valgrind之类的东西,或者查看更多扩展列表。
如果我们没有消息来源,说实话我不知道...
运行它们一千次,测量实际花费的时间,然后取平均结果。这应该可以消除由于服务器上运行其他应用程序而引起的差异。
我可能倾向于将" time -o somefile"添加到系统命令的前面,然后将其添加到主程序计时得到的总时间中。除非我必须做很多次,否则我会找到一种方法来获取两次时间输出并将它们添加到屏幕上(使用awk或者shell或者perl或者其他东西)。
/ usr / bin / time(bash中不是内置的" time")可以提供一些有趣的统计信息。
$ /usr/bin/time -v xeyes Command being timed: "xeyes" User time (seconds): 0.00 System time (seconds): 0.01 Percent of CPU this job got: 0% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:04.57 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 0 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 9 Minor (reclaiming a frame) page faults: 517 Voluntary context switches: 243 Involuntary context switches: 0 Swaps: 0 File system inputs: 1072 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0
我似乎终于找到了它。
姓名
时间获取处理时间
概要
#包括
clock_t times(struct tms *buf);
描述
times()将当前处理时间存储在buf的struct tms中
指着。结构tms如以下定义:
struct tms { clock_t tms_utime; /* user time */ clock_t tms_stime; /* system time */ clock_t tms_cutime; /* user time of children */ clock_t tms_cstime; /* system time of children */ };
儿童时间是所有等待儿童的递归总和。
我想知道为什么它还没有成为标准的CLI实用程序。也许我只是一无所知。