Created
September 12, 2018 12:23
-
-
Save nbibler/51fe108322d26683e816cbf5d30eda78 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
require 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'benchmark-memory', require: false | |
end | |
require 'benchmark' | |
require 'benchmark/memory' | |
HASH = { | |
frozen: 'test', | |
mutable: 'test'.dup | |
} | |
ENCODING = 'utf-8' | |
LOOPS = 500_000 | |
def frozen_conditional | |
HASH[:frozen] = 'test' | |
HASH[:frozen] = HASH[:frozen].dup if HASH[:frozen].frozen? | |
HASH[:frozen].force_encoding(ENCODING) | |
end | |
def frozen_non_conditional | |
HASH[:frozen] = 'test' | |
HASH[:frozen].dup.force_encoding(ENCODING) | |
end | |
def mutable_conditional | |
HASH[:mutable] = HASH[:mutable].dup if HASH[:mutable].frozen? | |
HASH[:mutable].force_encoding(ENCODING) | |
end | |
def mutable_non_conditional | |
HASH[:mutable].dup.force_encoding(ENCODING) | |
end | |
Benchmark.bmbm do |x| | |
x.report('frozen - conditional') do | |
LOOPS.times do | |
frozen_conditional | |
end | |
end | |
x.report('frozen - non-conditional') do | |
LOOPS.times do | |
frozen_non_conditional | |
end | |
end | |
x.report('mutable - conditional') do | |
LOOPS.times do | |
mutable_conditional | |
end | |
end | |
x.report('mutable - non-conditional') do | |
LOOPS.times do | |
mutable_non_conditional | |
end | |
end | |
end | |
Benchmark.memory do |x| | |
x.report('frozen - conditional') do | |
LOOPS.times do | |
frozen_conditional | |
end | |
end | |
x.report('frozen - non-conditional') do | |
LOOPS.times do | |
frozen_non_conditional | |
end | |
end | |
x.report('mutable - conditional') do | |
LOOPS.times do | |
mutable_conditional | |
end | |
end | |
x.report('mutable - non-conditional') do | |
LOOPS.times do | |
mutable_non_conditional | |
end | |
end | |
x.compare! | |
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
Rehearsal ------------------------------------------------------------- | |
frozen - conditional 0.283732 0.000318 0.284050 ( 0.284053) | |
frozen - non-conditional 0.197853 0.000612 0.198465 ( 0.198556) | |
mutable - conditional 0.092148 0.000082 0.092230 ( 0.092258) | |
mutable - non-conditional 0.159169 0.000099 0.159268 ( 0.159304) | |
---------------------------------------------------- total: 0.734013sec | |
user system total real | |
frozen - conditional 0.240769 0.000155 0.240924 ( 0.241009) | |
frozen - non-conditional 0.177855 0.000123 0.177978 ( 0.178045) | |
mutable - conditional 0.093638 0.000108 0.093746 ( 0.093857) | |
mutable - non-conditional 0.153172 0.000057 0.153229 ( 0.153260) |
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
Calculating ------------------------------------- | |
frozen - conditional 20.000M memsize ( 40.000 retained) | |
500.000k objects ( 1.000 retained) | |
1.000 strings ( 1.000 retained) | |
frozen - non-conditional | |
20.000M memsize ( 0.000 retained) | |
500.000k objects ( 0.000 retained) | |
1.000 strings ( 0.000 retained) | |
mutable - conditional | |
0.000 memsize ( 0.000 retained) | |
0.000 objects ( 0.000 retained) | |
0.000 strings ( 0.000 retained) | |
mutable - non-conditional | |
20.000M memsize ( 0.000 retained) | |
500.000k objects ( 0.000 retained) | |
1.000 strings ( 0.000 retained) | |
Comparison: | |
mutable - conditional: 0 allocated | |
frozen - conditional: 20000000 allocated - Infx more | |
frozen - non-conditional: 20000000 allocated - Infx more | |
mutable - non-conditional: 20000000 allocated - Infx more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment