2010-08-30 48 views
2

我正在處理項目歐拉問題。但是需要相當長的時間(上次30分鐘)才能讓我的電腦得到答案。Ruby程序幾乎所有的時間都是從sys時間開始的?

在我的Linux PC上執行時間命令時。我得到這樣的結果:

real 1m42.417s 
user 0m18.204s 
sys 1m24.026s 

這是一個基於比問題要小得多的數據集的時間。

所以我的問題是,這次是結果表明我可以做一些事情來優化我的程序。我的猜測是GC在大多數時間工作,或者一些標準庫函數來自ruby需要很長時間才能找藉口。另外請注意,沒有太多的I/O參與,程序將在最後打印結果。

根據反饋從AboutRuby添加輪廓結果:

% cumulative self    self  total 
time seconds seconds calls ms/call ms/call name 
66.03  9.70  9.70 10508  0.92  1.26 Array#find_index 
24.10 13.24  3.54 1027032  0.00  0.00 Fixnum#== 
    4.90 13.96  0.72  1046  0.69 13.38 Array#each 
    1.97 14.25  0.29  525  0.55 29.05 Integer#upto 
    0.61 14.34  0.09  9322  0.01  0.01 BasicObject#!= 
    0.48 14.41  0.07  7733  0.01  0.01 Fixnum#% 
    0.34 14.46  0.05 10508  0.00  0.00 BasicObject#== 
    0.27 14.50  0.04 10715  0.00  0.00 Fixnum#/ 
    0.27 14.54  0.04  523  0.08  1.19 Object#find_div 
    0.20 14.57  0.03  8411  0.00  0.00 Fixnum#>= 
    0.14 14.59  0.02  8383  0.00  0.00 Fixnum#<= 
    0.14 14.61  0.02  526  0.04  0.04 Class#new 
    0.14 14.63  0.02  523  0.04  0.10 Enumerable.inject 
    0.14 14.65  0.02  3206  0.01  0.01 Array#<< 
    0.14 14.67  0.02 11245  0.00  0.00 Fixnum#+ 
    0.07 14.68  0.01  523  0.02  0.02 Fixnum#> 
    0.07 14.69  0.01  8134  0.00  0.00 Fixnum#- 
    0.00 14.69  0.00  4  0.00  0.00 IO#set_encoding 
    0.00 14.69  0.00  523  0.00  0.00 Float#floor 
    0.00 14.69  0.00  523  0.00  0.00 Math.sqrt 
    0.00 14.69  0.00  523  0.00  0.00 Fixnum#to_f 
    0.00 14.69  0.00  5  0.00  0.00 Module#method_added 
    0.00 14.69  0.00  526  0.00  0.00 Array#initialize 
    0.00 14.69  0.00  1  0.00 700.00 Object#find_abundants 
    0.00 14.69  0.00  2  0.00  0.00 Kernel.require 
    0.00 14.69  0.00  523  0.00 26.71 Object#can_sum 
    0.00 14.69  0.00  2  0.00  0.00 Kernel.gem_original_require 
    0.00 14.69  0.00  73  0.00  0.00 Hash#default 
    0.00 14.69  0.00  1  0.00 13990.00 Object#search_for_can 
    0.00 14.69  0.00  246  0.00  0.00 Fixnum#to_s 
    0.00 14.69  0.00  246  0.00  0.00 Kernel.inspect 
    0.00 14.69  0.00  1  0.00  0.00 Array#inspect 
    0.00 14.69  0.00  1  0.00  0.00 Kernel.p 
    0.00 14.69  0.00  1  0.00 14690.00 #toplevel 

============================== ================================================== ==== 這是新的頂級耗時的函數調用(後速度可達3分鐘):

% cumulative self    self  total 
time seconds seconds calls ms/call ms/call name 
30.19 19.52  19.52 4313353  0.00  0.01 Set#include? 
28.38 37.87  18.35 56246  0.33  0.68 Hash#each_key 
15.96 48.19  10.32 28125  0.37  2.97 Integer#upto 
    7.55 53.07  4.88 271292  0.02  0.02 Set#add 
    6.40 57.21  4.14 4313353  0.00  0.00 Hash#include? 
    2.23 58.65  1.44 28123  0.05  0.85 Object#find_div 
    1.52 59.63  0.98 271292  0.00  0.00 Hash#[]= 
    1.28 60.46  0.83 56246  0.01  0.69 Set#each 
    1.25 61.27  0.81 56253  0.01  0.04 Class#new 
    1.16 62.02  0.75 237659  0.00  0.00 Fixnum#+ 
    1.08 62.72  0.70 28125  0.02  0.05 Set#initialize 
    0.84 63.26  0.54 28124  0.02  0.18 Enumerable.inject 
    0.63 63.67  0.41 28123  0.01  0.02 Math.sqrt 
    0.23 63.82  0.15 28125  0.01  0.01 Hash#initialize 
    0.22 63.96  0.14 28123  0.00  1.23 Object#can_sum 
    0.19 64.08  0.12 28123  0.00  0.00 Float#floor 
    0.17 64.19  0.11  2 55.00 115.00 Array#each 
    0.15 64.29  0.10  1 100.00 210.00 Array#inspect 
    0.14 64.38  0.09 56246  0.00  0.00 Kernel.block_given? 
    0.12 64.46  0.08 28123  0.00  0.00 Fixnum#to_f 
    0.12 64.54  0.08 28124  0.00  0.00 NilClass#nil? 
    0.12 64.62  0.08  6966  0.01  0.02 Kernel.inspect 
    0.05 64.65  0.03  6966  0.00  0.00 Fixnum#to_s 
    0.00 64.65  0.00  3  0.00  0.00 Array#initialize 
    0.00 64.65  0.00  1  0.00  0.00 Kernel.respond_to? 

大概可以用於進一步加速,或者嘗試改進算法呢?

+0

使用set而不是Array來包含?根據這個功能: http://stackoverflow.com/questions/410623/convert-an-array-into-an-index-hash-in-ruby 結束了約10倍加快,懶惰和提交項目歐拉的結果是正確的。 – 2010-08-30 10:24:14

回答

1

所有項目歐拉問題(至少我知道的)在幾乎任何編程語言中都可以在合理的時間內解決。程序運行需要30分鐘,這表明你的算法不好。

+0

我正在研究這個問題:-D – 2010-08-30 09:06:28

+0

我的觀點是,當我看到它有必要的時候,我從系統時間的前5次加速時,我會進一步改進算法。 – 2010-08-30 10:12:47

+2

我意識到最終哪種數據結構(數組,集合,哈希)並不比一個更好的算法重要。在閱讀了其他人如何實現他們的算法後,我重寫了我的算法。而且我的新程序實際上在幾秒鐘內運行,沒有太多的優化工作。 所以我想你是對的先生,在我的情況下,算法比 數據結構或其他語言實現細節重要得多。 – 2010-09-04 03:49:38

0

那麼,系統中的大部分時間通常是表明它正在等待IO。但是,如果你說IO沒有太多,那可能不是問題。但是,它仍然在sys上花費大量時間,因此建議進行一些其他系統調用。

如果您製作的對象數量非常大,則需要時間來釋放所有這些對象。它是否打印結果,然後在最終返回到shell之前花時間完成?我曾經遇到過這樣的問題,一個蜘蛛程序進入了一個令人討厭的循環,最終使用了近4個內存。該計劃關閉了幾分鐘。

你可以做的另一件事是benchmark程序和程序的一小部分。您也可以運行profiler以查看哪些代碼運行得最多。

+0

根據Profiler的結果,看起來像Array#find_index,可能我會嘗試哈希? 不知道如何進行基準測試。 – 2010-08-30 09:11:08

+0

另外它看起來沒有太多時間爲我分配許多對象的內存。 – 2010-08-30 10:11:33

相關問題