-
-
Save PragTob/17f7dcab51ad98a3f064 to your computer and use it in GitHub Desktop.
define_method meta benchmark
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
require 'benchmark/ips' | |
class FakeDimension | |
def initialize(margin_start) | |
@margin_start = margin_start | |
@margin_start_relative = relative? @margin_start | |
end | |
def relative?(result) | |
result.is_a?(Float) && result <= 1 | |
end | |
def calculate_relative(result) | |
(result * 100).to_i | |
end | |
define_method :full_meta do | |
instance_variable_name = '@' + :margin_start.to_s | |
value = instance_variable_get(instance_variable_name) | |
value = calculate_relative value if relative? value | |
value | |
end | |
IVAR_NAME = "@margin_start" | |
define_method :hoist_ivar_name do | |
value = instance_variable_get(IVAR_NAME) | |
value = calculate_relative value if relative? value | |
value | |
end | |
define_method :direct_ivar do | |
value = @margin_start | |
value = calculate_relative value if relative? value | |
value | |
end | |
eval <<-CODE | |
def full_string | |
value = @margin_start | |
value = calculate_relative value if relative? value | |
value | |
end | |
CODE | |
def full_direct | |
value = @margin_start | |
value = calculate_relative value if relative? value | |
value | |
end | |
end | |
def do_benchmark(description, margin_start) | |
puts description | |
Benchmark.ips do |benchmark| | |
dim = FakeDimension.new margin_start | |
benchmark.report("full_meta") { dim.full_meta } | |
benchmark.report("hoist_ivar_name") { dim.hoist_ivar_name } | |
benchmark.report("direct_ivar") { dim.direct_ivar } | |
benchmark.report("full_string") { dim.full_string } | |
benchmark.report("full_direct") { dim.full_direct } | |
benchmark.compare! | |
end | |
end | |
do_benchmark('Non relative margin start', 10) | |
do_benchmark('Relative margin start', 0.8) |
For completeness, jruby 9.0.0.0:
tobi@airship ~/Desktop $ ruby meta_benchmark.rb
Non relative margin start
Calculating -------------------------------------
full_meta 87.548k i/100ms
hoist_ivar_name 130.467k i/100ms
direct_ivar 156.668k i/100ms
full_string 171.480k i/100ms
full_direct 171.436k i/100ms
-------------------------------------------------
full_meta 2.579M (± 6.3%) i/s - 12.870M
hoist_ivar_name 3.231M (± 8.2%) i/s - 16.047M
direct_ivar 5.560M (± 6.6%) i/s - 27.730M
full_string 7.027M (± 5.0%) i/s - 34.982M
full_direct 7.034M (±11.9%) i/s - 34.459M
Comparison:
full_direct: 7034295.3 i/s
full_string: 7027336.0 i/s - 1.00x slower
direct_ivar: 5559686.4 i/s - 1.27x slower
hoist_ivar_name: 3231384.6 i/s - 2.18x slower
full_meta: 2579060.4 i/s - 2.73x slower
Relative margin start
Calculating -------------------------------------
full_meta 105.522k i/100ms
hoist_ivar_name 120.094k i/100ms
direct_ivar 152.049k i/100ms
full_string 162.297k i/100ms
full_direct 159.890k i/100ms
-------------------------------------------------
full_meta 2.316M (± 5.6%) i/s - 11.607M
hoist_ivar_name 2.942M (± 3.7%) i/s - 14.772M
direct_ivar 4.549M (± 6.6%) i/s - 22.655M
full_string 5.664M (± 2.8%) i/s - 28.240M
full_direct 5.666M (± 3.7%) i/s - 28.301M
Comparison:
full_direct: 5665954.8 i/s
full_string: 5663913.6 i/s - 1.00x slower
direct_ivar: 4549031.3 i/s - 1.25x slower
hoist_ivar_name: 2942342.7 i/s - 1.93x slower
full_meta: 2315864.0 i/s - 2.45x slower
And the newly released ruby 9.0.3.0 with some related improvements (7-15% better for some cases and almost the performance as the eval
for direct_ivar now):
tobi@airship ~/Desktop $ ruby meta_benchmark.rb
Non relative margin start
Calculating -------------------------------------
full_meta 87.493k i/100ms
hoist_ivar_name 135.651k i/100ms
direct_ivar 163.963k i/100ms
full_string 165.898k i/100ms
full_direct 165.800k i/100ms
-------------------------------------------------
full_meta 2.754M (± 4.2%) i/s - 13.736M
hoist_ivar_name 3.682M (± 4.1%) i/s - 18.449M
direct_ivar 6.953M (± 8.7%) i/s - 34.268M
full_string 7.145M (± 4.4%) i/s - 35.668M
full_direct 7.165M (± 4.1%) i/s - 35.813M
Comparison:
full_direct: 7164776.1 i/s
full_string: 7144531.3 i/s - 1.00x slower
direct_ivar: 6952823.3 i/s - 1.03x slower
hoist_ivar_name: 3682185.7 i/s - 1.95x slower
full_meta: 2753789.7 i/s - 2.60x slower
Relative margin start
Calculating -------------------------------------
full_meta 105.745k i/100ms
hoist_ivar_name 122.527k i/100ms
direct_ivar 148.861k i/100ms
full_string 145.214k i/100ms
full_direct 150.985k i/100ms
-------------------------------------------------
full_meta 2.367M (± 7.4%) i/s - 11.843M
hoist_ivar_name 2.882M (± 4.7%) i/s - 14.458M
direct_ivar 5.317M (± 4.1%) i/s - 26.497M
full_string 5.335M (±11.2%) i/s - 26.139M
full_direct 5.490M (± 4.8%) i/s - 27.479M
Comparison:
full_direct: 5489869.4 i/s
full_string: 5335464.0 i/s - 1.03x slower
direct_ivar: 5317361.0 i/s - 1.03x slower
hoist_ivar_name: 2881805.3 i/s - 1.91x slower
full_meta: 2367465.1 i/s - 2.32x slower
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on my machine with 2.2.2:
And with jruby-1.7.21: