Skip to content

Instantly share code, notes, and snippets.

@284km
Created December 17, 2018 11:29
Show Gist options
  • Save 284km/e20efe3d3329486d3549ec7b96d1f769 to your computer and use it in GitHub Desktop.
Save 284km/e20efe3d3329486d3549ec7b96d1f769 to your computer and use it in GitHub Desktop.
# ruby 2.6.0preview3 (2018-11-06 trunk 65578) [x86_64-darwin17]
# 
# 
# Warming up --------------------------------------
#       Without Freeze   372.719k i/100ms
#          With Freeze   451.989k i/100ms
# Calculating -------------------------------------
#       Without Freeze     14.312M (± 2.3%) i/s -     71.562M in   5.002940s
#          With Freeze     23.070M (± 3.5%) i/s -    115.257M in   5.003053s
# 
# Comparison:
#          With Freeze: 23070279.5 i/s
#       Without Freeze: 14312418.6 i/s - 1.61x  slower
# 

require "benchmark/ips"

# Allocates new string over and over again
def without_freeze
  "To freeze or not to freeze"
end

# Keeps and reuses shared string
def with_feeze
  "To freeze or not to freeze".freeze
end

Benchmark.ips do |x|
  x.report("Without Freeze") { without_freeze }
  x.report("With Freeze") { with_feeze }
  x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment