Ruby profiler结果解释
我正在使用Ruby profiler结果解释,ruby,ruby-prof,Ruby,Ruby Prof,我正在使用ruby profgem来分析我的代码 结果如下所示: %self total self wait child calls name 50.56 31.06 23.45 0.00 7.62 234593 Array#each 14.29 6.62 6.62 0.00 0.00 562480 Array#- 13.63 6.32 6.32 0
ruby prof
gem来分析我的代码
结果如下所示:
%self total self wait child calls name
50.56 31.06 23.45 0.00 7.62 234593 Array#each
14.29 6.62 6.62 0.00 0.00 562480 Array#-
13.63 6.32 6.32 0.00 0.00 157816 Array#|
11.20 5.20 5.20 0.00 0.00 6210903 Hash#default
2.44 46.36 1.13 0.00 46.36 78909 Analyzer#process
2.02 46.36 0.94 0.00 46.36 78908 Analyzer#try
1.70 0.79 0.79 0.00 0.00 562481 UnboundMethod#bind
1.53 7.34 0.71 0.00 6.62 562480 Method#call
1.18 0.55 0.55 0.00 0.00 562480 Kernel#instance_variable_defined?
0.76 46.36 0.35 0.00 46.36 6250 Array#combination
0.37 0.17 0.17 0.00 0.00 105763 Array#concat
0.25 25.19 0.12 0.00 25.07 77842 Enumerable#group_by
0.02 46.36 0.01 0.00 46.36 3125 Enumerator#each
0.02 0.01 0.01 0.00 0.00 78908 Array#empty?
...
我确信我的代码不会试图访问某些散列中不存在的密钥。
问题是-什么是Hash#default
可能意味着什么
下面是一段代码:
class Analyzer
def process(level, hypo_right, hypo_wrong)
if portion = @portions[level]
selections = @selections[level] - hypo_wrong
master_size = selections.size
selections -= hypo_right
new_portion = portion + selections.size - master_size
if new_portion > selections.size || new_portion < 0
return
elsif new_portion == 0
try(level, hypo_right, [], hypo_wrong, selections)
else
selections.combination(new_portion).each do |hypo_right2|
try(level, hypo_right, hypo_right2, hypo_wrong, (selections - hypo_right2))
end
end
else
puts hypo_right.inspect
end
end
def try(level, right, right2, wrong, wrong2)
local_right = right | right2
local_wrong = wrong | wrong2
right2.each { |r| local_wrong.concat(@siblings[r]) }
unless wrong2.empty?
grouped_wrong = local_wrong.group_by{|e| @vars[e] }
wrong2.each do |w|
qid = @vars[w]
if grouped_wrong[qid].size == @limits[qid]
local_right << (@q_hash[qid] - grouped_wrong[qid])[0]
end
end
end
process(level + 1, local_right, local_wrong)
end
def start
process(0, [], [])
end
end
类分析器
def流程(液位、准右、准错)
如果部分=@部分[级别]
选择=@selections[level]-hypo_错误
master_size=selections.size
选择-=次右
新建部分=部分+选择。大小-主部分大小
如果新建部分>selections.size | |新建部分<0
回来
elsif新_部分==0
尝试(级别、次选项正确、[]次选项错误、选择)
其他的
选择。组合(新的部分)。每个都有右下角2|
尝试(级别、次右、次右2、次错,(选择-次右2))
终止
终止
其他的
把hypo_放到右边
终止
终止
def try(级别、正确、正确2、错误、错误2)
局部右=右|右2
本地错误=错误|错误2
right2.each{r | local|u error.concat(@siblines[r]))
除非是错的2.空的?
分组错误=局部错误。分组错误由{e |@vars[e]}
错误2.每个人都做|
qid=@vars[w]
如果分组错误[qid]。大小==@限制[qid]
局部右多亏了里夫拉夫,找到了答案:
require 'ruby-prof'
h = (0..9).inject({}) {|h, x| h[x] = (x+97).chr;h }
a = (0..1000000).collect { rand(100) }
RubyProf.start
g = a.group_by {|x| h[x/10] }
RubyProf::FlatPrinter.new(RubyProf.stop).print(STDOUT)
Thread ID: 17188993880
Total: 1.210938
%self total self wait child calls name
100.00 1.21 1.21 0.00 0.00 1 Array#each
0.00 0.00 0.00 0.00 0.00 10 Hash#default
0.00 1.21 0.00 0.00 1.21 1 Enumerable#group_by
0.00 1.21 0.00 0.00 1.21 1 Object#irb_binding
很难说没有足够的源代码来重现这种行为。你是仅仅使用ruby,还是实际使用rails?Taryn East,这不是rails代码。调用哈希#默认值的groupby
不是吗?你用的是哪种红宝石?里夫拉夫,也许你是对的。6210903/77842=79,这可能是一个平均错误大小。