-
-
Save palkan/637dc83edd86d70b5dbf72f2a4d702e5 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
require "benchmark_driver" | |
source = <<~RUBY | |
class Hash | |
def symbolize_keys | |
transform_keys { |key| key.to_sym rescue key } | |
end | |
def refined_symbolize_keys | |
transform_keys { |key| key.to_sym rescue key } | |
end | |
end | |
module HashRefinements | |
refine Hash do | |
def refined_symbolize_keys | |
raise "never called" | |
end | |
end | |
end | |
HASH = {foo: 1, bar: 2, baz: 3} | |
class Foo | |
def original | |
end | |
def refined | |
end | |
end | |
module FooRefinements | |
refine Foo do | |
def refined | |
raise "never called" | |
end | |
end | |
end | |
FOO = Foo.new | |
RUBY | |
Benchmark.driver do |x| | |
x.prelude %Q{ | |
#{source} | |
} | |
x.report "#symbolize_keys original", %{ HASH.symbolize_keys } | |
x.report "#symbolize_keys refined", %{ HASH.refined_symbolize_keys } | |
end | |
Benchmark.driver do |x| | |
x.prelude %Q{ | |
#{source} | |
} | |
x.report "no-op original", %{ FOO.original } | |
x.report "no-op refined", %{ FOO.refined } | |
end |
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
$ ruby -v | |
jruby 9.2.20.1 (2.5.8) 2021-11-30 2a2962fbd1 OpenJDK 64-Bit Server VM 25.312-b07 on 1.8.0_312-b07 +jit [linux-x86_64] | |
$ ruby tmp/refbench.rb | |
Warming up -------------------------------------- | |
#symbolize_keys original 1.579M i/s - 1.575M times in 0.997675s (633.51ns/i) | |
#symbolize_keys refined 2.490M i/s - 2.504M times in 1.005468s (401.62ns/i) | |
Calculating ------------------------------------- | |
#symbolize_keys original 2.546M i/s - 4.736M times in 1.860168s (392.81ns/i) | |
#symbolize_keys refined 2.819M i/s - 7.470M times in 2.649830s (354.74ns/i, 993clocks/i) | |
Comparison: | |
#symbolize_keys refined: 2818932.7 i/s | |
#symbolize_keys original: 2545741.6 i/s - 1.11x slower | |
Warming up -------------------------------------- | |
no-op original 34.305M i/s - 31.927M times in 0.930692s (29.15ns/i, 81clocks/i) | |
no-op refined 33.971M i/s - 31.579M times in 0.929581s (29.44ns/i, 82clocks/i) | |
Calculating ------------------------------------- | |
no-op original 156.589M i/s - 102.914M times in 0.657221s (6.39ns/i, 17clocks/i) | |
no-op refined 150.137M i/s - 101.912M times in 0.678796s (6.66ns/i, 18clocks/i) | |
Comparison: | |
no-op original: 156588963.7 i/s | |
no-op refined: 150136562.3 i/s - 1.04x slower |
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
$ ruby -v | |
ruby 2.6.9p207 (2021-11-24 revision 67954) [x86_64-linux] | |
$ ruby tmp/refbench.rb | |
Warming up -------------------------------------- | |
#symbolize_keys original 2.446M i/s - 2.440M times in 0.997688s (408.87ns/i) | |
#symbolize_keys refined 2.472M i/s - 2.471M times in 0.999584s (404.49ns/i) | |
Calculating ------------------------------------- | |
#symbolize_keys original 2.615M i/s - 7.337M times in 2.805984s (382.43ns/i) | |
#symbolize_keys refined 2.679M i/s - 7.417M times in 2.768364s (373.26ns/i) | |
Comparison: | |
#symbolize_keys refined: 2679081.2 i/s | |
#symbolize_keys original: 2614889.2 i/s - 1.02x slower | |
Warming up -------------------------------------- | |
no-op original 23.002M i/s - 22.394M times in 0.973545s (43.47ns/i, 121clocks/i) | |
no-op refined 24.372M i/s - 23.677M times in 0.971450s (41.03ns/i, 114clocks/i) | |
Calculating ------------------------------------- | |
no-op original 49.758M i/s - 69.007M times in 1.386839s (20.10ns/i, 56clocks/i) | |
no-op refined 49.622M i/s - 73.117M times in 1.473481s (20.15ns/i, 56clocks/i) | |
Comparison: | |
no-op original: 49758465.9 i/s | |
no-op refined: 49622214.3 i/s - 1.00x slower |
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
$ ruby -v | |
ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux] | |
$ ruby tmp/refbench.rb | |
Warming up -------------------------------------- | |
#symbolize_keys original 2.095M i/s - 2.095M times in 0.999938s (477.29ns/i) | |
#symbolize_keys refined 2.163M i/s - 2.166M times in 1.001197s (462.23ns/i) | |
Calculating ------------------------------------- | |
#symbolize_keys original 2.278M i/s - 6.285M times in 2.758778s (438.92ns/i) | |
#symbolize_keys refined 2.264M i/s - 6.490M times in 2.866516s (441.67ns/i) | |
Comparison: | |
#symbolize_keys original: 2278339.7 i/s | |
#symbolize_keys refined: 2264153.1 i/s - 1.01x slower | |
Warming up -------------------------------------- | |
no-op original 22.508M i/s - 21.865M times in 0.971437s (44.43ns/i, 124clocks/i) | |
no-op refined 22.203M i/s - 21.563M times in 0.971159s (45.04ns/i, 126clocks/i) | |
Calculating ------------------------------------- | |
no-op original 63.358M i/s - 67.523M times in 1.065739s (15.78ns/i, 44clocks/i) | |
no-op refined 64.178M i/s - 66.610M times in 1.037887s (15.58ns/i, 43clocks/i) | |
Comparison: | |
no-op refined: 64178338.5 i/s | |
no-op original: 63357980.1 i/s - 1.01x slower | |
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
$ ruby -v | |
ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux] | |
$ ruby tmp/refbench.rb | |
Warming up -------------------------------------- | |
#symbolize_keys original 2.374M i/s - 2.370M times in 0.998293s (421.21ns/i) | |
#symbolize_keys refined 2.016M i/s - 2.009M times in 0.996745s (496.08ns/i) | |
Calculating ------------------------------------- | |
#symbolize_keys original 2.513M i/s - 7.122M times in 2.834496s (397.98ns/i) | |
#symbolize_keys refined 2.152M i/s - 6.047M times in 2.810691s (464.77ns/i) | |
Comparison: | |
#symbolize_keys original: 2512718.7 i/s | |
#symbolize_keys refined: 2151591.1 i/s - 1.17x slower | |
Warming up -------------------------------------- | |
no-op original 21.041M i/s - 20.485M times in 0.973564s (47.53ns/i, 133clocks/i) | |
no-op refined 10.314M i/s - 10.174M times in 0.986488s (96.96ns/i, 271clocks/i) | |
Calculating ------------------------------------- | |
no-op original 56.027M i/s - 63.123M times in 1.126657s (17.85ns/i, 49clocks/i) | |
no-op refined 14.846M i/s - 30.941M times in 2.084072s (67.36ns/i, 188clocks/i) | |
Comparison: | |
no-op original: 56026979.5 i/s | |
no-op refined: 14846195.6 i/s - 3.77x slower |
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
$ ruby -v | |
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux] | |
$ ruby tmp/refbench.rb | |
Warming up -------------------------------------- | |
#symbolize_keys original 2.133M i/s - 2.126M times in 0.996866s (468.87ns/i) | |
#symbolize_keys refined 1.876M i/s - 1.872M times in 0.998076s (533.09ns/i) | |
Calculating ------------------------------------- | |
#symbolize_keys original 2.339M i/s - 6.398M times in 2.735345s (427.50ns/i) | |
#symbolize_keys refined 2.011M i/s - 5.628M times in 2.798318s (497.26ns/i) | |
Comparison: | |
#symbolize_keys original: 2339166.3 i/s | |
#symbolize_keys refined: 2011040.6 i/s - 1.16x slower | |
Warming up -------------------------------------- | |
no-op original 21.244M i/s - 20.630M times in 0.971102s (47.07ns/i, 131clocks/i) | |
no-op refined 9.891M i/s - 9.744M times in 0.985155s (101.10ns/i, 283clocks/i) | |
Calculating ------------------------------------- | |
no-op original 58.117M i/s - 63.732M times in 1.096622s (17.21ns/i, 48clocks/i) | |
no-op refined 14.708M i/s - 29.673M times in 2.017444s (67.99ns/i, 190clocks/i) | |
Comparison: | |
no-op original: 58116993.1 i/s | |
no-op refined: 14708422.7 i/s - 3.95x slower |
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
$ ruby -v | |
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-linux] | |
$ ruby tmp/refbench.rb | |
Warming up -------------------------------------- | |
#symbolize_keys original 2.443M i/s - 2.432M times in 0.995489s (409.38ns/i) | |
#symbolize_keys refined 2.025M i/s - 2.024M times in 0.999262s (493.78ns/i) | |
Calculating ------------------------------------- | |
#symbolize_keys original 2.372M i/s - 7.328M times in 3.088909s (421.51ns/i) | |
#symbolize_keys refined 1.941M i/s - 6.076M times in 3.130070s (515.19ns/i) | |
Comparison: | |
#symbolize_keys original: 2372420.1 i/s | |
#symbolize_keys refined: 1941019.0 i/s - 1.22x slower | |
Warming up -------------------------------------- | |
no-op original 19.388M i/s - 18.660M times in 0.962420s (51.58ns/i, 144clocks/i) | |
no-op refined 9.277M i/s - 9.090M times in 0.979792s (107.79ns/i, 301clocks/i) | |
Calculating ------------------------------------- | |
no-op original 51.791M i/s - 58.165M times in 1.123064s (19.31ns/i, 54clocks/i) | |
no-op refined 14.457M i/s - 27.831M times in 1.925187s (69.17ns/i, 193clocks/i) | |
Comparison: | |
no-op original: 51790974.2 i/s | |
no-op refined: 14456518.9 i/s - 3.58x slower |
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
$ ruby -v | |
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [arm64-darwin21] | |
$ ruby tmp/refbench.rb | |
Warming up -------------------------------------- | |
#symbolize_keys original 4.012M i/s - 4.250M times in 1.059341s (249.28ns/i) | |
#symbolize_keys refined 3.748M i/s - 3.895M times in 1.039306s (266.80ns/i) | |
Calculating ------------------------------------- | |
#symbolize_keys original 4.362M i/s - 12.035M times in 2.758954s (229.25ns/i) | |
#symbolize_keys refined 4.000M i/s - 11.244M times in 2.811282s (250.02ns/i) | |
Comparison: | |
#symbolize_keys original: 4362026.0 i/s | |
#symbolize_keys refined: 3999755.6 i/s - 1.09x slower | |
Warming up -------------------------------------- | |
no-op original 31.525M i/s - 31.604M times in 1.002492s (31.72ns/i) | |
no-op refined 18.618M i/s - 18.909M times in 1.015654s (53.71ns/i) | |
Calculating ------------------------------------- | |
no-op original 80.631M i/s - 94.576M times in 1.172948s (12.40ns/i) | |
no-op refined 29.182M i/s - 55.853M times in 1.913975s (34.27ns/i) | |
Comparison: | |
no-op original: 80631273.5 i/s | |
no-op refined: 29181553.6 i/s - 2.76x slower |
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
$ ruby -v | |
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [aarch64-linux] | |
$ ruby tmp/refbench.rb | |
Warming up -------------------------------------- | |
#symbolize_keys original 3.876M i/s - 3.942M times in 1.017151s (258.02ns/i) | |
#symbolize_keys refined 3.865M i/s - 3.951M times in 1.022232s (258.71ns/i) | |
Calculating ------------------------------------- | |
#symbolize_keys original 4.135M i/s - 11.627M times in 2.812119s (241.86ns/i) | |
#symbolize_keys refined 4.138M i/s - 11.596M times in 2.801981s (241.64ns/i) | |
Comparison: | |
#symbolize_keys refined: 4138435.7 i/s | |
#symbolize_keys original: 4134660.2 i/s - 1.00x slower | |
Warming up -------------------------------------- | |
no-op original 18.022M i/s - 18.333M times in 1.017246s (55.49ns/i) | |
no-op refined 29.850M i/s - 30.300M times in 1.015059s (33.50ns/i) | |
Calculating ------------------------------------- | |
no-op original 73.301M i/s - 54.067M times in 0.737609s (13.64ns/i) | |
no-op refined 70.178M i/s - 89.551M times in 1.276058s (14.25ns/i) | |
Comparison: | |
no-op original: 73300578.1 i/s | |
no-op refined: 70177555.0 i/s - 1.04x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment