Skip to content

Instantly share code, notes, and snippets.

@NickLaMuro
Last active October 3, 2017 21:56
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 NickLaMuro/a91406e512a59c5b676f110ce968680b to your computer and use it in GitHub Desktop.
Save NickLaMuro/a91406e512a59c5b676f110ce968680b to your computer and use it in GitHub Desktop.
Performance benchmarks on `ActiveSupport`'s `String#camelize` and `String#underscore`
#!/usr/bin/env ruby
#
# This script has been adapted from a similar script by Dave Gynn (@dgynn):
#
# - https://gist.github.com/dgynn/bd54e970f8942ce2ca532e4adc3fd30a
#
# This can be run against a local version of ActiveSupport or the latest
# release for comparison (ideally, you would put this in your `rails/`
# dir locally and run against local changes from there)
#
# Run at least once with --install-gems to ensure all required Gems are installed
#
# ruby ./test_acronym_fixes.rb --install-gems
#
# If you wish to change gems (based on options, see below), you must re-run
# with --install-gems
#
#
#
# To run against local code
#
# ruby ./test_acronym_fixes.rb --local
#
# To run against latest release
#
# ruby ./test_acronym_fixes.rb
#
begin
require "bundler/inline"
require "optparse"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
### Option Parsing ###
options = { :install_gems => false, :local => false }
ARGV.options do |opts|
opts.banner = "Usage: #{File.basename $PROGRAM_NAME} [options]"
opts.on("--install-gems", "Bundle install the gems. Run with this flag once.") do |install|
options[:install_gems] = install
end
opts.on("--local", "Run against local code rather than installed gem") do |local|
options[:local] = local
end
opts.on("--silent-bundler", "Don't display bundler output") do
require "bundler"
options[:ui] = Bundler::UI::Silent.new
end
opts.on("-h", "--help", "Show this help") { puts opts; exit }
opts.parse!
end
### Dependency Handling ###
gemfile(options[:install_gems], :ui => options[:ui]) do
source "https://rubygems.org"
if options[:local]
gem "activesupport", :path => "./activesupport"
else
gem "activesupport"
end
gem "memory_profiler"
gem "get_process_mem"
end
### Main Script ###
require "benchmark"
require "active_support/inflector"
capitals = ("A".."Z").to_a
lowers = ("a".."z").to_a
string = "AbcDef"
output = StringIO.new
report = nil
benchmark = Benchmark.measure do
report = MemoryProfiler.report(:color_output => true) do
10_000.times do
string.gsub!(/[A-Z]/) {|char| capitals[(capitals.index(char) + 1) % 26] }
.gsub!(/[a-z]/) {|char| lowers[(lowers.index(char) + 1) % 26] }
.underscore
.camelize
end
end
end
### Benchmark Output ###
report.pretty_print(output)
output.rewind
20.times { puts output.readline }
puts
real_seconds = benchmark.real % 60
user_seconds = benchmark.utime % 60
sys_seconds = benchmark.stime % 60
cu_seconds = benchmark.cutime % 60
cs_seconds = benchmark.cstime % 60
puts ""
puts "Timings"
puts "-------"
puts "real %dm%.3fs" % [(benchmark.real - real_seconds) / 60, real_seconds]
puts "user %dm%.3fs" % [(benchmark.utime - user_seconds) / 60, user_seconds]
puts "sys %dm%.3fs" % [(benchmark.stime - sys_seconds) / 60, sys_seconds]
puts "cuser %dm%.3fs" % [(benchmark.cutime - cu_seconds) / 60, cu_seconds]
puts "csys %dm%.3fs" % [(benchmark.cstime - cs_seconds) / 60, cs_seconds]
puts
puts "Memory used MB: #{GetProcessMem.new.mb}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment