Skip to content

Instantly share code, notes, and snippets.

@Erol
Last active December 17, 2015 13:29
Show Gist options
  • Save Erol/5617833 to your computer and use it in GitHub Desktop.
Save Erol/5617833 to your computer and use it in GitHub Desktop.
Benchmark: Tap vs Non-Tap
require 'benchmark'
def tap_method
result = rand.tap {}
end
def non_tap_method
result = rand
result
end
n = 100_000
Benchmark.bm(10) do |bm|
bm.report('tap') { n.times { tap_method } }
bm.report('non-tap') { n.times { no_tap_method } }
end
user system total real
tap 0.030000 0.000000 0.030000 ( 0.029819)
non-tap 0.020000 0.000000 0.020000 ( 0.018391)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment