Created
November 17, 2020 18:33
-
-
Save tomascco/2999e1fe5febe689a861595968d47797 to your computer and use it in GitHub Desktop.
blocks vs lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
Lambda = ->n { n.even? } | |
Benchmark.ips do |x| | |
x.report('map with lambda literal') { (1..1_000).map(&->n { n.even? }) } | |
x.report('map with lambda constant') { (1..1_000).map(&Lambda) } | |
x.report('map with block') { (1..1_000).map { |n| n.even? } } | |
x.report('map with &') { (1..1_000).map(&:even?) } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment