Ruby / Performance

From WhyNotWiki

Jump to: navigation, search

Ruby / Performance  edit   (Category  edit)


Contents

[edit] Performance of the Ruby interpreter

http://shootout.alioth.debian.org/debian/benchmark.php?test=all&lang=ruby&lang2=java

[edit] Benchmarking

[edit] Benchmark (stdlib)

http://www.ruby-doc.org/stdlib/libdoc/benchmark/rdoc/

    require 'benchmark'

    puts Benchmark.measure { "a"*1_000_000 }

On my machine (FreeBSD 3.2 on P5, 100MHz) this generates:

    1.166667   0.050000   1.216667 (  0.571355)

This report shows the user CPU time, system CPU time, the sum of the user and system CPU times, and the elapsed real time. The unit of time is seconds.

Benchmark.measure(label = "") {|| ...}

Returns the time used to execute the given block as a Benchmark::Tms object.

Benchmark::Tms#to_a()

Returns a new 6-element array, consisting of the label, user CPU time, system CPU time, children‘s user CPU time, children‘s system CPU time and elapsed real time.

Benchmark.realtime() {|| ...}

Returns the elapsed real time used to execute the given block.


[edit] Profiling

[edit] Silhouette

Project/Development: http://rubyforge.org/projects/silhouette/


Description: A 2 stage profiler.




Readiness: 2.0.0 April 21, 2006


[edit] Ruby-Prof (>= 0.4)

Homepage: http://ruby-prof.rubyforge.org/
Documentation: http://ruby-prof.rubyforge.org/ ruby-prof command
Source code: gem install ruby-prof
Project/Development: http://rubyforge.org/projects/ruby-prof
As listed in other directories: http://cfis.savagexi.com/articles/2006/06/21/ruby-prof-0-4-0-with-call-graphs (blog)
Description: ruby-prof is a fast code profiler for Ruby.


Implementation language: C, Ruby
Authors: Shugo Maeda
Readiness: 4 - Beta


[edit] Features

Its features include: [1]

  • Speed - it is a C extension and therefore many times faster than the standard Ruby profiler.
  • Flat Profiles - similar to the reports generated by the standard Ruby profiler example report
  • Graph profiles - similar to GProf, these show how long a method runs, which methods call it and which methods it calls. example report
  • HTML Graph profiles example report
  • Threads - supports profiling multiple threads simultaneously
  • Recursive calls - supports profiling recursive method calls
  • Reports - can generate both text and cross-referenced html reports
  • Output - can output to standard out or to a file

[edit] Reading the report

http://on-ruby.blogspot.com/2006/08/profile-and-ruby-prof.html

%self  cumulative total  self  children  calls self/call total/call  name
 23.64     0.13   0.13   0.13  0.00     10002   0.00     0.00     String#split
 21.82     0.30   0.39   0.12  0.27     10002   0.00     0.00     CallRot#read_line
 18.18     0.55   0.54   0.10  0.44         1   0.10     0.54     #foreach
 12.73     0.42   0.07   0.07  0.00     10002   0.00     0.00     #local
  5.45     0.33   0.05   0.03  0.02     10002   0.00     0.00     CallRot#line_is_future?
  3.64     0.44   0.02   0.02  0.00     10003   0.00     0.00     #allocate
  3.64     0.35   0.02   0.02  0.00     20004   0.00     0.00     Array#pop
  3.64     0.17   0.02   0.02  0.00     10002   0.00     0.00     Comparable#>
  ...

self and children break the total time into more discrete measures. self shows how much time is spent in the method itself, [excluding] calls out to child methods. children shows how much time is spent in calls to those child methods.

[edit] Code sample (usage)

[2]

  require 'ruby-prof'

  # Profile the code
  result = RubyProf.profile do
    ...
    [code to profile]
    ...
  end

  # Print a graph profile to text
  printer = RubyProf::GraphPrinter.new(result)
  #printer = RubyProf::FlatPrinter.new(result)
  printer.print(STDOUT, 0)



require 'rubygems'
require 'ruby-prof'
#require 'rubyprof_ext' # From Rails

10000.times do
  'a b c'.split
end

result = RubyProf.profile do
  10000.times do
    [1, 2, 3] & [1, 3]
  end
end

printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT, 0)

> ruby slow.rb
Thread ID: -604175388
 %self  cumulative  total     self   children  calls self/call total/call  name
 31.25     0.05      0.16     0.05     0.11        1     0.05     0.16     Integer#times
 68.75     0.16      0.11     0.11     0.00    10000     0.00     0.00     Array#&
  0.00     0.16      0.16     0.00     0.16        1     0.00     0.16     #toplevel

[edit] Ruby-Prof 0.3 [deprecated (category)]

Source code: http://shugo.net/archive/ruby-prof/
Project/Development: http://raa.ruby-lang.org/project/ruby-prof





Authors: Charlie Savage, Shugo Maeda


It seems that this older version is what is used by ActionProfiler and perhaps others...

Differences include: It has a slightly different API. Its namespace is Prof instead of RubyProf. It's not packaged as a gem and is required via require 'prof' rather than require 'ruby-prof'.

Prof.print_profile @profile_data, $stderr

printer = Prof::FlatPrinter.new(result)
# uninitialized constant Prof::FlatPrinter 
Facts about Ruby / PerformanceRDF feed
Implemented in C, Ruby  +
Description [Oops! No type defined for attribute]
Personal tools