Last active
August 29, 2015 14:22
-
-
Save tncbbthositg/bafc1437234e3d04b730 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[1, 0, 2, 1, 0, 2, 1, 0, 2, 1] | |
[1, 0, 2, 1, 0, 2, 1, 0, 2, 1] | |
[1, 0, 2, 1, 0, 2, 1, 0, 2, 1] | |
user system total real | |
eager_map 0.370000 0.010000 0.380000 ( 0.373325) | |
lazy_map 1.130000 0.010000 1.140000 ( 1.142926) | |
composed 0.540000 0.000000 0.540000 ( 0.545507) | |
user system total real | |
eager_map x 1 0.000000 0.000000 0.000000 ( 0.000013) | |
eager_map x 10 0.000000 0.000000 0.000000 ( 0.000015) | |
eager_map x 100 0.000000 0.000000 0.000000 ( 0.000050) | |
eager_map x 1000 0.000000 0.000000 0.000000 ( 0.000334) | |
eager_map x 10000 0.000000 0.000000 0.000000 ( 0.003235) | |
eager_map x 100000 0.040000 0.000000 0.040000 ( 0.038602) | |
eager_map x 1000000 0.330000 0.010000 0.340000 ( 0.333256) | |
eager_map x 10000000 3.650000 0.080000 3.730000 ( 3.748070) | |
user system total real | |
lazy_map x 1 0.000000 0.000000 0.000000 ( 0.000038) | |
lazy_map x 10 0.000000 0.000000 0.000000 ( 0.000035) | |
lazy_map x 100 0.000000 0.000000 0.000000 ( 0.000114) | |
lazy_map x 1000 0.000000 0.000000 0.000000 ( 0.000887) | |
lazy_map x 10000 0.030000 0.000000 0.030000 ( 0.033165) | |
lazy_map x 100000 0.100000 0.020000 0.120000 ( 0.116230) | |
lazy_map x 1000000 0.980000 0.000000 0.980000 ( 0.980008) | |
lazy_map x 10000000 10.010000 0.020000 10.030000 ( 10.066494) | |
user system total real | |
composed x 1 0.000000 0.000000 0.000000 ( 0.000023) | |
composed x 10 0.000000 0.000000 0.000000 ( 0.000012) | |
composed x 100 0.000000 0.000000 0.000000 ( 0.000066) | |
composed x 1000 0.000000 0.000000 0.000000 ( 0.000498) | |
composed x 10000 0.010000 0.000000 0.010000 ( 0.004791) | |
composed x 100000 0.050000 0.000000 0.050000 ( 0.048782) | |
composed x 1000000 0.510000 0.000000 0.510000 ( 0.515904) | |
composed x 10000000 5.410000 0.050000 5.460000 ( 5.484562) |
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'benchmark' | |
DOUBLE = -> (i) { 2 * i } | |
ADD_TWO = -> (i) { 2 + i } | |
MOD_THREE = -> (i) { i % 3 } | |
COMPOSED = -> (i) do | |
MOD_THREE.call(ADD_TWO.call(DOUBLE.call(i))) | |
end | |
def get_enumerator count | |
i = 0 | |
Enumerator.new do |y| | |
y.yield i while count >= i += 1 | |
end | |
end | |
def demo | |
p eager_map(get_enumerator 10).to_a | |
p composed(get_enumerator 10).to_a | |
p lazy_map(get_enumerator 10).to_a | |
puts | |
end | |
def eager_map items | |
items.map(&DOUBLE).map(&ADD_TWO).map(&MOD_THREE) | |
end | |
def lazy_map items | |
items.lazy.map(&DOUBLE).map(&ADD_TWO).map(&MOD_THREE) | |
end | |
def composed items | |
items.map &COMPOSED | |
end | |
demo | |
n = 1_000_000 | |
Benchmark.bm do |x| | |
x.report('eager_map') { eager_map(get_enumerator n).count } | |
x.report('lazy_map') { lazy_map(get_enumerator n).count } | |
x.report('composed') { composed(get_enumerator n).count } | |
end | |
[:eager_map, :lazy_map, :composed].each do |method| | |
puts | |
Benchmark.bm(22) do |x| | |
(0..7).each do |e| | |
x.report("#{method} x #{10 ** e}") { self.send(method, get_enumerator(10 ** e)).count } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment