Skip to content

Instantly share code, notes, and snippets.

@brhoades
Created March 23, 2017 15:56
Show Gist options
  • Save brhoades/a4cad529df95992f5443d145c6fb40c6 to your computer and use it in GitHub Desktop.
Save brhoades/a4cad529df95992f5443d145c6fb40c6 to your computer and use it in GitHub Desktop.
ruby lambdas / procs / blocks
bare block:
Measure Mode: wall_time
Thread ID: 70093342368220
Fiber ID: 70093351182580
Total: 1.095921
Sort by: self_time
%self total self wait child calls name
84.49 0.926 0.926 0.000 0.000 1 Array#map
15.50 0.170 0.170 0.000 0.000 2 Integer#times
0.00 1.096 0.000 0.000 1.096 1 Global#[No method]
0.00 0.170 0.000 0.000 0.170 1 Enumerator#each
0.00 0.170 0.000 0.000 0.170 1 Enumerable#to_a
* indicates recursively called methods
========================================
Proc:
Measure Mode: wall_time
Thread ID: 70093342368220
Fiber ID: 70093351182580
Total: 3.384458
Sort by: self_time
%self total self wait child calls name
48.35 1.636 1.636 0.000 0.000 5000000 Proc#call
46.72 3.218 1.581 0.000 1.636 1 Array#map
4.93 0.167 0.167 0.000 0.000 2 Integer#times
0.00 3.384 0.000 0.000 3.384 1 Global#[No method]
0.00 0.167 0.000 0.000 0.167 1 Enumerator#each
0.00 0.167 0.000 0.000 0.167 1 Enumerable#to_a
* indicates recursively called methods
========================================
Lambda:
Measure Mode: wall_time
Thread ID: 70093342368220
Fiber ID: 70093351182580
Total: 3.315006
Sort by: self_time
%self total self wait child calls name
49.08 1.627 1.627 0.000 0.000 5000000 Proc#call
45.82 3.146 1.519 0.000 1.627 1 Array#map
5.09 0.169 0.169 0.000 0.000 2 Integer#times
0.00 3.315 0.000 0.000 3.315 1 Global#[No method]
0.00 0.169 0.000 0.000 0.169 1 Enumerable#to_a
0.00 0.169 0.000 0.000 0.169 1 Enumerator#each
* indicates recursively called methods
========================================
method:
Measure Mode: wall_time
Thread ID: 70093342368220
Fiber ID: 70093351182580
Total: 2.962332
Sort by: self_time
%self total self wait child calls name
51.35 2.801 1.521 0.000 1.279 1 Array#map
43.18 1.279 1.279 0.000 0.000 5000000 Object#meth
5.46 0.162 0.162 0.000 0.000 2 Integer#times
0.00 2.962 0.000 0.000 2.962 1 Global#[No method]
0.00 0.162 0.000 0.000 0.162 1 Enumerable#to_a
0.00 0.162 0.000 0.000 0.162 1 Enumerator#each
* indicates recursively called methods
========================================
require 'ruby-prof'
proc = Proc.new { |x| x * 2 }
lam = lambda { |x| x * 2 }
def meth(x)
x * 2
nil
end
results = {}
times = 5000000
RubyProf.measure_mode = RubyProf::WALL_TIME
RubyProf.start
(times).times.to_a.map { |x| x * 2 }
results["bare block"] = RubyProf.stop
RubyProf.start
(times).times.to_a.map { |x| proc.call(x) }
results["Proc"] = RubyProf.stop
RubyProf.start
(times).times.to_a.map { |x| lam.call(x) }
results["Lambda"] = RubyProf.stop
RubyProf.start
(times).times.to_a.map { |x| meth(x) }
results["method"] = RubyProf.stop
results.each do |k, v|
puts "#{k}:"
RubyProf::FlatPrinter.new(v).print(STDOUT)
puts "="*40
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment