在大多数情况下,您可以通过消除瓶颈来提高慢速程序的性能。探查器是找到瓶颈的工具。

为了向您的Ruby程序添加配置文件,您需要首先使用命令行options -r profile 加载 Profile 库。

使用语法

$ruby -r profile [programfile] [arguments]

Example

这是从 hello.rb 文件生成的输出,但这不会给您带来太多的想法,因此,您可以尝试使用更大的程序。输出以小字体显示。

[root@ruby]# ruby -r profile hello.rb
Hello, Mac!
   %   cumulative     self         self           total
   time    seconds  seconds      calls    ms/call  ms/call  name
   0.00     0.00      0.00        2       0.00     0.00    IO#write
   0.00     0.00      0.00        2       0.00     0.00    Module#method_added
   0.00     0.00      0.00        1       0.00     0.00    Hello#hello
   0.00     0.00      0.00        1       0.00     0.00    Hello#initialize
   0.00     0.00      0.00        1       0.00     0.00    Class#inherited
   0.00     0.00      0.00        1       0.00     0.00    Kernel.puts
   0.00     0.00      0.00        1       0.00     0.00    Class#new
   0.00     0.01      0.00        1       0.00    10.00    #toplevel

参考链接

https://www.learnfk.com/ruby/ruby-profiler.html