Last active
January 31, 2022 16:23
-
-
Save emptyflask/adf6e15d505ad49efe3769c65554b20f to your computer and use it in GitHub Desktop.
Benchmark questionable code!
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/ips' | |
require 'benchmark/memory' | |
# IPS = iterations per second | |
# https://github.com/evanphx/benchmark-ips | |
string = "123" | |
%i[ips memory].each do |bench| | |
puts bench.upcase | |
Benchmark.send(bench) do |x| | |
x.report("string match") { string == "123" } | |
x.report("classname match") { string.class.name == "String" } | |
x.report("kind_of?") { string.kind_of?(String) } | |
x.report("is_a?") { string.is_a?(String) } | |
x.report("instance_of?") { string.instance_of?(String) } | |
x.compare! | |
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
IPS | |
Warming up -------------------------------------- | |
string match 1.273M i/100ms | |
classname match 843.890k i/100ms | |
kind_of? 1.722M i/100ms | |
is_a? 1.726M i/100ms | |
instance_of? 1.716M i/100ms | |
Calculating ------------------------------------- | |
string match 13.426M (± 2.3%) i/s - 67.481M in 5.028951s | |
classname match 8.578M (± 1.7%) i/s - 43.038M in 5.019080s | |
kind_of? 17.539M (± 2.7%) i/s - 87.847M in 5.012759s | |
is_a? 17.578M (± 1.8%) i/s - 88.048M in 5.010710s | |
instance_of? 17.442M (± 1.7%) i/s - 87.517M in 5.019161s | |
Comparison: | |
is_a?: 17577715.9 i/s | |
kind_of?: 17538697.9 i/s - same-ish: difference falls within error | |
instance_of?: 17441575.0 i/s - same-ish: difference falls within error | |
string match: 13425571.1 i/s - 1.31x (± 0.00) slower | |
classname match: 8577522.9 i/s - 2.05x (± 0.00) slower | |
MEMORY | |
Calculating ------------------------------------- | |
string match 40.000 memsize ( 0.000 retained) | |
1.000 objects ( 0.000 retained) | |
1.000 strings ( 0.000 retained) | |
classname match 40.000 memsize ( 40.000 retained) | |
1.000 objects ( 1.000 retained) | |
1.000 strings ( 1.000 retained) | |
kind_of? 0.000 memsize ( 0.000 retained) | |
0.000 objects ( 0.000 retained) | |
0.000 strings ( 0.000 retained) | |
is_a? 0.000 memsize ( 0.000 retained) | |
0.000 objects ( 0.000 retained) | |
0.000 strings ( 0.000 retained) | |
instance_of? 0.000 memsize ( 0.000 retained) | |
0.000 objects ( 0.000 retained) | |
0.000 strings ( 0.000 retained) | |
Comparison: | |
kind_of?: 0 allocated | |
is_a?: 0 allocated - same | |
instance_of?: 0 allocated - same | |
string match: 40 allocated - Infx more | |
classname match: 40 allocated - Infx more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment