簡體   English   中英

C:減少運行時間以進行優化

[英]C: reducing runtime for optimization

我的程序的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()多次運行代碼,因此其運行時間足以獲取樣本。

然后,您可以嘗試我最喜歡的技術 它會比gprof更多信息。

PS不用擔心CPU百分比。 它只說明您的計算機是否繁忙並且沒有執行太多I / O。 它不會告訴您有關程序的任何信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM