Last active
August 30, 2017 18:56
-
-
Save serradura/266b8e34fe0495e3bcb9cad1ca41d51c to your computer and use it in GitHub Desktop.
Ruby FP playground
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
# gem install benchmark-ips | |
require "benchmark/ips" | |
######### | |
# shout # | |
######### | |
Benchmark.ips do |x| | |
x.report("inline") { "#{'foo'.upcase}!" } | |
x.report("util") { TUtils.shout('fooz') } | |
x.report("util_2") { TUtils2.shout('fooz') } | |
x.report("instance") { TInstance.new('kooz').shout } | |
x.report("instance_2") { TInstance.new('tooz').shout } | |
x.report("func") { TFuncs::Shout.('baaz') } | |
x.report("func_2") { TFuncs2::Shout.('caaz') } | |
x.report("func__3") { TFuncs3::Shout.('ceez') } | |
x.report("func___4") { TFuncs3::Shout.('cooz') } | |
x.compare! | |
end; | |
# Warming up -------------------------------------- | |
# inline 176.820k i/100ms | |
# util 155.999k i/100ms | |
# util_2 126.719k i/100ms | |
# instance 123.668k i/100ms | |
# instance_2 122.304k i/100ms | |
# func 120.336k i/100ms | |
# func_2 121.618k i/100ms | |
# func__3 147.243k i/100ms | |
# func___4 145.803k i/100ms | |
# Calculating ------------------------------------- | |
# inline 3.625M (± 3.8%) i/s - 18.212M in 5.032169s | |
# util 2.955M (± 3.1%) i/s - 14.820M in 5.019701s | |
# util_2 2.013M (± 3.1%) i/s - 10.138M in 5.041329s | |
# instance 1.898M (± 3.3%) i/s - 9.522M in 5.022187s | |
# instance_2 1.899M (± 2.6%) i/s - 9.540M in 5.026581s | |
# func 1.937M (± 3.0%) i/s - 9.747M in 5.035761s | |
# func_2 1.921M (± 3.1%) i/s - 9.608M in 5.005715s | |
# func__3 2.521M (± 3.5%) i/s - 12.663M in 5.028931s | |
# func___4 2.549M (± 3.3%) i/s - 12.831M in 5.038733s | |
# Comparison: | |
# inline: 3624613.0 i/s | |
# util: 2955263.3 i/s - 1.23x slower | |
# func___4: 2549303.0 i/s - 1.42x slower | |
# func__3: 2521063.5 i/s - 1.44x slower | |
# util_2: 2012877.4 i/s - 1.80x slower | |
# func: 1937407.1 i/s - 1.87x slower | |
# func_2: 1921213.7 i/s - 1.89x slower | |
# instance_2: 1899140.5 i/s - 1.91x slower | |
# instance: 1898180.6 i/s - 1.91x slower | |
####################### | |
# shout frozen string # | |
####################### | |
Benchmark.ips do |x| | |
x.report("inline") { "#{'foo'.upcase}!" } | |
x.report("util") { TUtils.shout('froz'.freeze) } | |
x.report("util_2") { TUtils2.shout('froz'.freeze) } | |
x.report("instance") { TInstance.new('kroz'.freeze).shout } | |
x.report("instance_2") { TInstance.new('troz'.freeze).shout } | |
x.report("func") { TFuncs::Shout.('braz'.freeze) } | |
x.report("func_2") { TFuncs2::Shout.('craz'.freeze) } | |
x.report("func__3") { TFuncs3::Shout.('crez'.freeze) } | |
x.report("func___4") { TFuncs3::Shout.('croz'.freeze) } | |
x.compare! | |
end; | |
# Warming up -------------------------------------- | |
# inline 177.240k i/100ms | |
# util 167.112k i/100ms | |
# util_2 127.721k i/100ms | |
# instance 125.253k i/100ms | |
# instance_2 127.246k i/100ms | |
# func 122.832k i/100ms | |
# func_2 123.499k i/100ms | |
# func__3 152.475k i/100ms | |
# func___4 149.009k i/100ms | |
# Calculating ------------------------------------- | |
# inline 3.635M (± 3.2%) i/s - 18.256M in 5.028143s | |
# util 3.206M (± 3.5%) i/s - 16.043M in 5.010616s | |
# util_2 2.131M (± 2.7%) i/s - 10.729M in 5.037493s | |
# instance 2.051M (± 2.6%) i/s - 10.271M in 5.010912s | |
# instance_2 2.040M (± 3.2%) i/s - 10.307M in 5.058705s | |
# func 2.037M (± 2.7%) i/s - 10.195M in 5.008696s | |
# func_2 2.052M (± 2.8%) i/s - 10.250M in 4.999829s | |
# func__3 2.765M (± 3.1%) i/s - 13.875M in 5.022070s | |
# func___4 2.776M (± 3.1%) i/s - 14.007M in 5.051224s | |
# Comparison: | |
# inline: 3634513.4 i/s | |
# util: 3205783.7 i/s - 1.13x slower | |
# func___4: 2775581.2 i/s - 1.31x slower | |
# func__3: 2765470.8 i/s - 1.31x slower | |
# util_2: 2131351.1 i/s - 1.71x slower | |
# func_2: 2051834.3 i/s - 1.77x slower | |
# instance: 2051108.3 i/s - 1.77x slower | |
# instance_2: 2039550.0 i/s - 1.78x slower | |
# func: 2036978.6 i/s - 1.78x slower | |
############### | |
# crazy_shout # | |
############### | |
Benchmark.ips do |x| | |
x.report("inline") { "#{'send'.upcase}!" * 10 } | |
x.report("util") { TUtils.crazy_shout('dooz') } | |
x.report("util_2") { TUtils2.crazy_shout('dooz') } | |
x.report("instance") { TInstance.new('gooz').crazy_shout } | |
x.report("instance_2") { TInstance.new('taaz').crazy_shout } | |
x.report("func") { TFuncs::CrazyShout.('eaaz') } | |
x.report("func_2") { TFuncs2::CrazyShout.('faaz') } | |
x.report("func__3") { TFuncs3::CrazyShout.('faaz') } | |
x.report("func___4") { TFuncs3::CrazyShout.('taaz') } | |
x.compare! | |
end; | |
# Warming up -------------------------------------- | |
# inline 104.737k i/100ms | |
# util 93.544k i/100ms | |
# util_2 72.009k i/100ms | |
# instance 80.479k i/100ms | |
# instance_2 80.434k i/100ms | |
# func 62.140k i/100ms | |
# func_2 70.187k i/100ms | |
# func__3 64.336k i/100ms | |
# func___4 64.097k i/100ms | |
# Calculating ------------------------------------- | |
# inline 1.548M (± 3.5%) i/s - 7.751M in 5.013020s | |
# util 1.321M (± 2.9%) i/s - 6.642M in 5.033694s | |
# util_2 932.930k (± 3.2%) i/s - 4.681M in 5.021894s | |
# instance 1.065M (± 2.7%) i/s - 5.392M in 5.067852s | |
# instance_2 1.067M (± 2.9%) i/s - 5.389M in 5.056662s | |
# func 792.895k (± 2.7%) i/s - 3.977M in 5.019264s | |
# func_2 911.658k (± 3.0%) i/s - 4.562M in 5.008367s | |
# func__3 802.842k (± 2.4%) i/s - 4.053M in 5.051472s | |
# func___4 796.053k (± 2.5%) i/s - 4.038M in 5.075871s | |
# Comparison: | |
# inline: 1547819.1 i/s | |
# util: 1320537.1 i/s - 1.17x slower | |
# instance_2: 1066639.0 i/s - 1.45x slower | |
# instance: 1064731.8 i/s - 1.45x slower | |
# util_2: 932929.8 i/s - 1.66x slower | |
# func_2: 911658.1 i/s - 1.70x slower | |
# func__3: 802842.2 i/s - 1.93x slower | |
# func___4: 796052.7 i/s - 1.94x slower | |
# func: 792894.9 i/s - 1.95x slower | |
############################# | |
# crazy_shout frozen string # | |
############################# | |
Benchmark.ips do |x| | |
x.report("inline") { "#{'send'.upcase}!" * 10 } | |
x.report("util") { TUtils.crazy_shout('droz'.freeze) } | |
x.report("util_2") { TUtils2.crazy_shout('droz'.freeze) } | |
x.report("instance") { TInstance.new('groz'.freeze).crazy_shout } | |
x.report("instance_2") { TInstance.new('traz'.freeze).crazy_shout } | |
x.report("func") { TFuncs::CrazyShout.('eraz'.freeze) } | |
x.report("func_2") { TFuncs2::CrazyShout.('fraz'.freeze) } | |
x.report("func__3") { TFuncs3::CrazyShout.('fraz'.freeze) } | |
x.report("func___4") { TFuncs3::CrazyShout.('traz'.freeze) } | |
x.compare! | |
end; | |
# Warming up -------------------------------------- | |
# inline 104.298k i/100ms | |
# util 95.838k i/100ms | |
# util_2 73.891k i/100ms | |
# instance 81.144k i/100ms | |
# instance_2 81.398k i/100ms | |
# func 64.352k i/100ms | |
# func_2 71.805k i/100ms | |
# func__3 65.829k i/100ms | |
# func___4 65.987k i/100ms | |
# Calculating ------------------------------------- | |
# inline 1.536M (± 3.8%) i/s - 7.718M in 5.033711s | |
# util 1.363M (± 5.3%) i/s - 6.804M in 5.006135s | |
# util_2 963.861k (± 3.2%) i/s - 4.877M in 5.064652s | |
# instance 1.104M (± 3.8%) i/s - 5.518M in 5.005082s | |
# instance_2 1.085M (± 4.4%) i/s - 5.454M in 5.034277s | |
# func 819.957k (± 3.6%) i/s - 4.119M in 5.030092s | |
# func_2 939.355k (± 4.0%) i/s - 4.739M in 5.053385s | |
# func__3 818.999k (± 2.9%) i/s - 4.147M in 5.067908s | |
# func___4 815.249k (± 2.6%) i/s - 4.091M in 5.021621s | |
# Comparison: | |
# inline: 1535527.2 i/s | |
# util: 1363281.4 i/s - 1.13x slower | |
# instance: 1104147.6 i/s - 1.39x slower | |
# instance_2: 1085442.4 i/s - 1.41x slower | |
# util_2: 963861.4 i/s - 1.59x slower | |
# func_2: 939355.4 i/s - 1.63x slower | |
# func: 819957.3 i/s - 1.87x slower | |
# func__3: 818998.6 i/s - 1.87x slower | |
# func___4: 815249.1 i/s - 1.88x slower |
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
module TFuncs | |
Compose = -> (f, g) { -> x { f.(g.(x)) }.freeze } | |
ToUpcase = -> str { str.upcase }.freeze | |
Exclaim = -> str { "#{str}!" }.freeze | |
Shout = Compose.(Exclaim, ToUpcase) | |
Repeat = -> (number, str) { str * number }.curry.freeze | |
CrazyShout = Compose.(Repeat.(10), Shout) | |
end | |
module TFuncs2 | |
ToUpcase = -> str { str.upcase }.freeze | |
Exclaim = -> str { "#{str}!" }.freeze | |
Shout = -> str { Exclaim.(ToUpcase.(str)) }.freeze | |
Repeat = -> (number, str) { str * number }.freeze | |
CrazyShout = -> str { Repeat.(10, Shout.(str)) }.freeze | |
end | |
module TFuncs3 | |
Compose = -> (f, g) { -> x { f.(g.(x)) }.freeze } | |
module ToUpcase | |
def self.call(str) | |
str.upcase | |
end | |
end | |
module Exclaim | |
def self.call(str) | |
"#{str}!" | |
end | |
end | |
Shout = Compose.(Exclaim, ToUpcase) | |
Repeat = -> (number, str) { str * number }.curry | |
CrazyShout = Compose.(Repeat.(10), Shout) | |
end | |
module TFunc4 | |
Compose = -> (f, g) { -> x { f.(g.(x)) }.freeze } | |
ToUpcase = -> str { str.upcase }.freeze | |
Exclaim = -> str { "#{str}!" }.freeze | |
Shout = Compose.(Exclaim, ToUpcase) | |
class Repeat | |
def initialize(number) | |
@number = number | |
end | |
def call(str) | |
str * @number | |
end | |
end | |
CrazyShout = Compose.(Repeat.new(10), Shout) | |
end |
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
class TInstance | |
def initialize(str) | |
@str = str | |
end | |
def shout | |
exclaim to_upcase(@str) | |
end | |
def crazy_shout | |
repeat 10, shout | |
end | |
private | |
def to_upcase(str) | |
str.upcase | |
end | |
def exclaim(str) | |
"#{str}!" | |
end | |
def repeat(number, str) | |
str * number | |
end | |
end | |
class TInstance2 | |
class Exclaim | |
def initialize(str) | |
@str = str | |
end | |
def to_s | |
"#{@str}!" | |
end | |
end | |
class Repeat | |
def initialize(number, str) | |
@number = number | |
@str = str | |
end | |
def to_s | |
str * @number | |
end | |
end | |
def initialize(str) | |
@str = str | |
end | |
def to_upcase | |
str.upcase | |
end | |
def shout | |
Exclaim.new(to_upcase).to_s | |
end | |
def crazy_shout | |
Repeat.new(10, shout).to_s | |
end | |
end |
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
module TUtils | |
extend self | |
def to_upcase(str) | |
str.upcase | |
end | |
def exclaim(str) | |
"#{str}!" | |
end | |
def shout(str) | |
exclaim to_upcase(str) | |
end | |
def repeat(number, str) | |
str * number | |
end | |
def crazy_shout(str) | |
repeat 10, shout(str) | |
end | |
end | |
module TUtils2 | |
extend self | |
def func(name, &block) | |
define_method(name, &block) | |
end | |
func(:to_upcase) { |str| str.upcase } | |
func(:exclaim) { |str| "#{str}!" } | |
func(:shout) { |str| exclaim to_upcase(str) } | |
func(:repeat) { |number, str| str * number } | |
func(:crazy_shout) { |str| repeat 10, shout(str) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment