Skip to content

Instantly share code, notes, and snippets.

@mechanicles
Created March 1, 2016 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mechanicles/da40a518e9c6593686cb to your computer and use it in GitHub Desktop.
Save mechanicles/da40a518e9c6593686cb to your computer and use it in GitHub Desktop.
Profile with freeze
require 'memory_profiler'
require 'active_support/core_ext/string'
def translate_using_freeze(key)
if key.to_s.first == '.'.freeze
"products/index".tr('/'.freeze, '.'.freeze)
end
end
report = MemoryProfiler.report(ignore_files: 'active_support/core_ext/string') do
30.times { translate_using_freeze('.name') }
end
report.pretty_print
# allocated memory by file
# -----------------------------------
# 3600 benchmark_with_freeze.rb
# allocated objects by file
# -----------------------------------
# 90 benchmark_with_freeze.rb
# Allocated String Report
# -----------------------------------
# 30 ".name"
# 30 benchmark_with_freeze.rb:11
#
# 30 "products.index"
# 30 benchmark_with_freeze.rb:6
#
# 30 "products/index"
# 30 benchmark_with_freeze.rb:6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment