C:减少运行时间以进行优化

时间:2013-11-11 05:35:28

标签: 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

感到困惑

1 个答案:

答案 0 :(得分:0)

根据你的另一个问题,你从8秒降到0.01秒。 那很不错。

现在,如果你想更进一步,首先要按照@Peter的评论建议。 在main()内多次运行代码,因此运行时间足以获取样本。

然后你可以尝试my favorite technique。 它比gprof提供更多信息。

P.S。不要担心CPU百分比。 它只是说明你的机器是否繁忙而没有做太多的I / O. 它没有告诉你关于你的程序的任何信息。