Skip to content

Instantly share code, notes, and snippets.

@ferrous26
Created July 31, 2011 02:20
Show Gist options
  • Select an option

  • Save ferrous26/1116284 to your computer and use it in GitHub Desktop.

Select an option

Save ferrous26/1116284 to your computer and use it in GitHub Desktop.
Simple #tap usage
require 'benchmark'
Benchmark.bmbm do |x|
x.report 'direct call' do
1_000_000.times do
x = true
x.nil?
x
end
end
x.report 'using tap Symbol#to_proc' do
1_000_000.times do
true.tap &:nil?
end
end
x.report 'using tap block' do
1_000_000.times do
true.tap { |x| x.nil? }
end
end
end
○ ruby bench.rb
Rehearsal ------------------------------------------------------------
direct call 0.140000 0.000000 0.140000 ( 0.160083)
using tap Symbol#to_proc 0.330000 0.000000 0.330000 ( 0.349109)
using tap block 0.260000 0.010000 0.270000 ( 0.295242)
--------------------------------------------------- total: 0.740000sec
user system total real
direct call 0.140000 0.000000 0.140000 ( 0.156700)
using tap Symbol#to_proc 0.340000 0.000000 0.340000 ( 0.364256)
using tap block 0.270000 0.000000 0.270000 ( 0.295129)
○ macruby bench.rb
Rehearsal ------------------------------------------------------------
direct call 0.100000 0.000000 0.100000 ( 0.114795)
using tap Symbol#to_proc 3.460000 0.270000 3.730000 ( 4.022874)
using tap block 0.400000 0.000000 0.400000 ( 0.446039)
--------------------------------------------------- total: 4.230000sec
user system total real
direct call 0.200000 0.030000 0.230000 ( 0.196450)
using tap Symbol#to_proc 4.650000 0.310000 4.960000 ( 4.815747)
using tap block 0.950000 0.040000 0.990000 ( 0.865683)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment