Ruby程序几乎所有时间都来自sys时间?

时间:2010-08-30 08:27:10

标签: ruby

我正在研究项目欧拉问题。但是我的电脑需要相当长的时间(上次30分钟)才能得到答案。

在我的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?

可能可以用于进一步加速,或尝试改进算法?

2 个答案:

答案 0 :(得分:1)

所有项目Euler问题(至少是我所知道的)在几乎任何编程语言中都能在合理的时间内解决。程序运行需要30分钟,表明你的算法不好。

答案 1 :(得分:0)

好吧,sys 中的大部分时间通常表明它正在等待IO。但是,如果你说没有多少IO,那可能不是问题。但是,它仍然在sys中花费了大量时间,所以建议其他一些系统调用。

如果你制作了大量的对象,则需要时间来释放所有这些对象。是否打印结果,然后在最终返回shell之前花些时间完成?曾经有一个问题,一个蜘蛛程序进入一个讨厌的循环,最终使用了近4个内存。该程序关闭需要几分钟时间。

你可以做的另一件事是benchmark程序,以及程序的一小部分。您还可以运行profiler以查看最常运行的代码。