我有一个宝石,做一些艰难的数字运算:它从图像中剔除"有趣"的部分。为此,我设置了几个算法。总的来说,它的性能很差;显然,我想改进:)。
我想测试和测量三件事:
- 内存使用
- cpu使用
- 在一个方法/例程中花费的总时间。
我想研究这个问题,并比较各种算法、参数和设置的值。
是否有一些Ruby功能,一个gem或类似的东西,允许我运行我的代码,改变一些参数或一小部分代码,再次运行它,然后比较结果?
我已经有了test:unit,应该已经就位了,顺便说一下,所以如果有使用这些测试框架的东西,那很好。
您可以使用标准的'profiler'库。它会报告每个方法所花费的时间。
require 'profiler'
def functionToBeProfiled
a = 0
1000.times do |i|
a = a + i * rand
end
end
Profiler__::start_profile
functionToBeProfiled
Profiler__::stop_profile
Profiler__::print_profile($stdout)
这会产生以下输出:
% cumulative self self total
time seconds seconds calls ms/call ms/call name
16.58 0.03 0.03 1 31.00 93.00 Integer#times
16.58 0.06 0.03 1000 0.03 0.03 Kernel.rand
8.56 0.08 0.02 3 5.33 10.33 RubyLex#identify_identifier
8.56 0.09 0.02 10 1.60 6.20 IRB::SLex::Node#match_io
8.56 0.11 0.02 84 0.19 0.19 Kernel.===
8.56 0.13 0.02 999 0.02 0.02 Float#+
8.56 0.14 0.02 6 2.67 5.33 String#gsub!
但是要小心,因为这个库会影响应用程序的性能。你从它得到的测量值只有在与你用同样的方法得到的其他测量值比较时才有用。它们不能用于评估绝对测量值。
我使用ruby-prof有很好的经验:
http://ruby-prof.rubyforge.org/在网上也有一个很好的关于各种分析方法的演示,但是我不记得标题和作者了,现在很难找到它…: - (
JRuby给了我惊喜。如果你的代码在这个实现上运行而没有改变,并且你熟悉Java基准测试软件,你应该看看(并让我知道你是如何做的)。
http://www.engineyard.com/blog/2010/monitoring-memory-with-jruby-part-1-jhat-and-visualvm/http://danlucraft.com/blog/2011/03/built - -分析器在jruby - 1.6/
现在我更仔细地阅读了你的问题,我意识到这并不能为你提供自动化过程的能力。