Last active
December 12, 2015 07:14
-
-
Save toch/ae18a67529f3333956a1 to your computer and use it in GitHub Desktop.
Example of usage of benchmark-lab on JuanitoFatas/fast-ruby
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
require 'benchmark/lab' | |
def method_names(number) | |
number.times.map do | |
10.times.inject("") { |e| e << ('a'..'z').to_a.sample} | |
end | |
end | |
class DefineMethod | |
def self.def_methods(_methods) | |
_methods.each do |method_name| | |
define_method method_name do | |
puts "win" | |
end | |
end | |
end | |
end | |
class ModuleEvalWithString | |
def self.def_methods(_methods) | |
_methods.each do |method_name| | |
module_eval %{ | |
def #{method_name} | |
puts "win" | |
end | |
} | |
end | |
end | |
end | |
def fast(input_size) | |
DefineMethod.def_methods(method_names(input_size)) | |
end | |
def slow(input_size) | |
ModuleEvalWithString.def_methods(method_names(input_size)) | |
end | |
nbr_of_samples = 20 | |
(1..10).each do |power| | |
input_size = 10**power | |
puts "Test for input size #{input_size}" | |
Benchmark.experiment(nbr_of_samples) do |x| | |
x.report("module_eval with string") { slow(input_size) } | |
x.report("define_method") { fast(input_size) } | |
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
-------------------------------------------------------------------------------- | |
Test for input size 10 | |
user system total real | |
module_eval with string [0.00,0.00,0.00] [0.00,0.00,0.00] [0.00,0.00,0.00] [0.00,0.00,0.00] | |
define_method [0.00,0.00,0.00] [0.00,0.00,0.00] [0.00,0.00,0.00] [0.00,0.00,0.00] | |
The best "module_eval with string" is not significantly (95%) better (total time). | |
-------------------------------------------------------------------------------- | |
Test for input size 100 | |
user system total real | |
module_eval with string [0.00,0.01,0.01] [0.00,0.00,0.00] [0.00,0.01,0.01] [0.01,0.01,0.01] | |
define_method [0.00,0.01,0.01] [0.00,0.00,0.00] [0.00,0.01,0.01] [0.01,0.01,0.01] | |
The best "module_eval with string" is not significantly (95%) better (total time). | |
-------------------------------------------------------------------------------- | |
Test for input size 1000 | |
user system total real | |
module_eval with string [0.08,0.08,0.09] [0.00,0.00,0.00] [0.08,0.08,0.09] [0.08,0.08,0.09] | |
define_method [0.07,0.08,0.09] [0.00,0.00,0.00] [0.07,0.08,0.09] [0.07,0.08,0.09] | |
The best "module_eval with string" is not significantly (95%) better (total time). | |
-------------------------------------------------------------------------------- | |
Test for input size 10000 | |
user system total real | |
module_eval with string [0.88,0.94,1.04] [0.00,0.00,0.01] [0.88,0.94,1.04] [0.88,0.94,1.04] | |
define_method [0.73,0.80,0.94] [0.00,0.00,0.00] [0.74,0.80,0.94] [0.74,0.80,0.94] | |
The best "define_method" is significantly (95%) better (total time). | |
-------------------------------------------------------------------------------- | |
Test for input size 100000 | |
user system total real | |
module_eval with string [12.85,14.36,22.65] [0.03,0.04,0.05] [12.89,14.40,22.69] [12.89,14.41,22.70] | |
define_method [25.57,29.93,33.74] [0.01,0.03,0.03] [25.59,29.97,33.75] [25.61,29.97,33.77] | |
The best "module_eval with string" is significantly (95%) better (total time). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment