C:减少优化运行时间

标签 c optimization time profiling gprof

我的程序的 gprof 输出如下:

Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    
  0.00      0.00     0.00    30002     0.00     0.00  insert
  0.00      0.00     0.00    10124     0.00     0.00  getNode
  0.00      0.00     0.00     3000     0.00     0.00  search
  0.00      0.00     0.00        1     0.00     0.00  initialize

我已经做了优化,运行时间是 0.01 秒(这是在我上传代码的服务器上计算的),这是我目前得到的最少时间。我无法进一步减少它,尽管我想这样做。我的程序的 0.01 秒运行时间与我在上面的 gprof 输出中看到的采样时间有什么关系吗? 调用图如下:

gprof -q ./a.out gmon.out 
             Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) no time propagated

index % time    self  children    called     name
                0.00    0.00   30002/30002       main [10]
[1]      0.0    0.00    0.00   30002         insert [1]
                0.00    0.00   10124/10124       getNode [2]
-----------------------------------------------
                0.00    0.00   10124/10124       insert [1]
[2]      0.0    0.00    0.00   10124         getNode [2]
-----------------------------------------------
                0.00    0.00    3000/3000        main [10]
[3]      0.0    0.00    0.00    3000         search [3]
-----------------------------------------------
                0.00    0.00       1/1           main [10]
[4]      0.0    0.00    0.00       1         initialize [4]
-----------------------------------------------
 While using `time /bin/sh -c ' ./a.out < inp.in '` on my machine I get below which varies slightly on every run .
real    0m0.024s
user    0m0.016s
sys         0m0.004s

real    0m0.017s
user    0m0.008s
sys     0m0.004s

我有点困惑如何关联 time 输出和 gprof o/p

最佳答案

根据您的其他问题,您将其从 8 秒缩短到了 0.01 秒。 这样就很不错了。

现在,如果您想更进一步,请首先按照@Peter 在评论中建议的方式进行操作。 在 main() 内多次运行代码,以便它运行足够长的时间来获取样本。

那你可以试试 my favorite technique 。 它将比 gprof 提供更多信息。

附注不用担心 CPU 百分比。 它仅表明您的机器是否繁忙并且没有执行太多 I/O。 它不会告诉您有关您的程序的任何信息。

关于C:减少优化运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19899536/

相关文章:

c - 从内存中读取数据mmap

java - 有没有办法让 Jruby 运行时实习所有字符串?

multithreading - 并行化: pthreads or OpenMP?

c# - 什么是最有效的时间测量方法?

web-services - 服务器通过 HTTPS 传输数据的重量有多大?

c - 欧拉计划 2# - 奇怪的输出

c - 为什么这个double类型变量作为函数参数传递时默认为int?

java - Java 8 中 try-with-resources block 中的锁风险

php - 使用 php 更改时间输出

c - 如何使用命令行参数读取二进制文件并在 C 中打印出来?