-
-
Save jbrechtel/5c5d2aba28be55cca3f7 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
[0, -30, 30, 0, -30, 30, 0, -30, 30, 0] | |
[0, -30, 30, 0, -30, 30, 0, -30, 30, 0] | |
[0, -30, 30, 0, -30, 30, 0, -30, 30, 0] | |
user system total real | |
eager_map 0.400000 0.010000 0.410000 ( 0.413597) | |
lazy_map 1.640000 0.000000 1.640000 ( 1.642215) | |
composed 0.610000 0.000000 0.610000 ( 0.610857) | |
m_composed 0.140000 0.000000 0.140000 ( 0.136136) | |
user system total real | |
eager_map x 1 0.000000 0.000000 0.000000 ( 0.000007) | |
eager_map x 10 0.000000 0.000000 0.000000 ( 0.000007) | |
eager_map x 100 0.000000 0.000000 0.000000 ( 0.000044) | |
eager_map x 1000 0.000000 0.000000 0.000000 ( 0.000415) | |
eager_map x 10000 0.000000 0.000000 0.000000 ( 0.004008) | |
eager_map x 100000 0.040000 0.000000 0.040000 ( 0.040525) | |
eager_map x 1000000 0.390000 0.020000 0.410000 ( 0.409117) | |
eager_map x 10000000 4.140000 0.100000 4.240000 ( 4.242423) | |
user system total real | |
lazy_map x 1 0.000000 0.000000 0.000000 ( 0.000036) | |
lazy_map x 10 0.000000 0.000000 0.000000 ( 0.000041) | |
lazy_map x 100 0.000000 0.000000 0.000000 ( 0.000179) | |
lazy_map x 1000 0.000000 0.000000 0.000000 ( 0.001815) | |
lazy_map x 10000 0.030000 0.010000 0.040000 ( 0.033293) | |
lazy_map x 100000 0.170000 0.000000 0.170000 ( 0.170410) | |
lazy_map x 1000000 1.700000 0.000000 1.700000 ( 1.705107) | |
lazy_map x 10000000 16.890000 0.000000 16.890000 ( 16.890536) | |
user system total real | |
composed x 1 0.000000 0.000000 0.000000 ( 0.000006) | |
composed x 10 0.000000 0.000000 0.000000 ( 0.000008) | |
composed x 100 0.000000 0.000000 0.000000 ( 0.000061) | |
composed x 1000 0.000000 0.000000 0.000000 ( 0.000636) | |
composed x 10000 0.000000 0.000000 0.000000 ( 0.006038) | |
composed x 100000 0.060000 0.000000 0.060000 ( 0.059965) | |
composed x 1000000 0.600000 0.000000 0.600000 ( 0.601747) | |
composed x 10000000 6.100000 0.010000 6.110000 ( 6.109109) | |
user system total real | |
m_composed x 1 0.000000 0.000000 0.000000 ( 0.000005) | |
m_composed x 10 0.000000 0.000000 0.000000 ( 0.000004) | |
m_composed x 100 0.000000 0.000000 0.000000 ( 0.000017) | |
m_composed x 1000 0.000000 0.000000 0.000000 ( 0.000137) | |
m_composed x 10000 0.000000 0.000000 0.000000 ( 0.001321) | |
m_composed x 100000 0.010000 0.000000 0.010000 ( 0.013195) | |
m_composed x 1000000 0.140000 0.000000 0.140000 ( 0.131730) | |
m_composed x 10000000 1.330000 0.030000 1.360000 ( 1.360293) |
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 } | |
TRIPLE = -> (i) { i * 3 } | |
TIMES_TEN = -> (i) { i * 10 } | |
MINUS_30 = -> (i) { i - 30 } | |
COMPOSED = -> (i) do | |
MINUS_30.call(TIMES_TEN.call(TRIPLE.call(MOD_THREE.call(ADD_TWO.call(DOUBLE.call(i)))))) | |
end | |
MANUAL_COMPOSITION = -> (i) do | |
((((((i * 2) + 2) % 3) * 3) * 10) - 30) | |
end | |
def get_enumerator count | |
i = 0 | |
Enumerator.new do |y| | |
y.yield i while count >= i += 1 | |
end | |
end | |
def get_array count | |
get_enumerator(count).to_a | |
end | |
def get_range count | |
(1..count) | |
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).map(&TRIPLE).map(&TIMES_TEN).map(&MINUS_30) | |
end | |
def lazy_map items | |
items.lazy.map(&DOUBLE).map(&ADD_TWO).map(&MOD_THREE).map(&TRIPLE).map(&TIMES_TEN).map(&MINUS_30) | |
end | |
def composed items | |
items.map &COMPOSED | |
end | |
def m_composed items | |
items.map &MANUAL_COMPOSITION | |
end | |
demo | |
strategy = :get_range | |
n = 1_000_000 | |
Benchmark.bm do |x| | |
x.report('eager_map') { eager_map(send(strategy, n)).count } | |
x.report('lazy_map') { lazy_map(send(strategy, n)).count } | |
x.report('composed') { composed(send(strategy, n)).count } | |
x.report('m_composed') { m_composed(send(strategy, n)).count } | |
end | |
[:eager_map, :lazy_map, :composed, :m_composed].each do |method| | |
puts | |
Benchmark.bm(22) do |x| | |
(0..7).each do |e| | |
x.report("#{method} x #{10 ** e}") { self.send(method, send(strategy, 10 ** e)).count } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment