Last active
August 29, 2015 14:17
-
-
Save philnash/daaeb46a7078723d9be3 to your computer and use it in GitHub Desktop.
A Benchmark of Array detect against lazy select and first
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
# A Benchmark of Array detect against lazy select and first | |
# | |
# Inspired by https://gist.github.com/ernie/1086770 | |
require 'benchmark' | |
haystack = (1..1_000_000).to_a | |
needles = 1.upto(100).map {|n| n * 10_000} | |
puts "\n" + '=' * 80 | |
puts "With in-block needle calculation" | |
puts '=' * 80 | |
Benchmark.bmbm(10) do |x| | |
x.report(:detect) do | |
100.times { |n| haystack.detect { |v| v == 10_000 * n } } | |
end | |
x.report(:lazy) do | |
100.times { |n| haystack.lazy.select { |v| v == 10_000 * n }.first } | |
end | |
x.report("select then first") do | |
100.times { |n| haystack.select { |v| v == 10_000 * n }.first } | |
end | |
end | |
puts "\n" + '=' * 80 | |
puts "With precalculated needles" | |
puts '=' * 80 | |
Benchmark.bmbm(10) do |x| | |
x.report(:detect) do | |
needles.each { |n| haystack.detect { |v| v == n } } | |
end | |
x.report(:lazy) do | |
needles.each { |n| haystack.lazy.select { |v| v == n }.first } | |
end | |
x.report("select then first") do | |
needles.each { |n| haystack.select { |v| v == n }.first } | |
end | |
end |
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
================================================================================ | |
With in-block needle calculation | |
================================================================================ | |
Rehearsal ----------------------------------------------------- | |
detect 5.650000 0.010000 5.660000 ( 5.694137) | |
lazy 6.660000 0.000000 6.660000 ( 6.696257) | |
select then first 8.650000 0.030000 8.680000 ( 8.705558) | |
------------------------------------------- total: 21.000000sec | |
user system total real | |
detect 5.810000 0.010000 5.820000 ( 5.862631) | |
lazy 7.280000 0.010000 7.290000 ( 7.361647) | |
select then first 9.240000 0.040000 9.280000 ( 9.326313) | |
================================================================================ | |
With precalculated needles | |
================================================================================ | |
Rehearsal ----------------------------------------------------- | |
detect 4.410000 0.000000 4.410000 ( 4.423517) | |
lazy 6.260000 0.010000 6.270000 ( 6.305923) | |
select then first 6.570000 0.020000 6.590000 ( 6.601375) | |
------------------------------------------- total: 17.270000sec | |
user system total real | |
detect 4.430000 0.010000 4.440000 ( 4.444339) | |
lazy 6.500000 0.010000 6.510000 ( 6.577266) | |
select then first 6.880000 0.030000 6.910000 ( 6.918666) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment