Skip to content

Instantly share code, notes, and snippets.

@mikeauclair
Last active May 22, 2017 16:31
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 mikeauclair/766ea0048cbe91fb4cda69934daf06d8 to your computer and use it in GitHub Desktop.
Save mikeauclair/766ea0048cbe91fb4cda69934daf06d8 to your computer and use it in GitHub Desktop.
require 'ruby-prof'
def profile_as(name)
if ENV['PROFILE']
RubyProf.start
yield
result = RubyProf.stop
printer = RubyProf::CallTreePrinter.new(result)
printer.print(:path => '.', :profile => name)
else
yield
end
end
def get_hash
output = {}
(1..10_000).each {|num| output[num] = num*2}
output
end
profile_as('better') do
the_hash = get_hash
(1..10_000).map do |num|
the_hash[num].to_f
end.map do |num|
num.to_s
end
end
events: wall_time
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Enumerator::Generator#initialize
0 3
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Enumerator::Lazy#map
0 14
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Enumerator::Generator#initialize
calls=2 30
30 3
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Enumerable#lazy
0 4
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Range#each
0 3039
fl=/Users/mauclair/Documents/profiling_preso/hurray.rb
fn=Object#get_hash
20 4
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Range#each
calls=1 22
22 3039
fl=/Users/mauclair/Documents/profiling_preso/hurray.rb
fn=Object#profile_as
8 43
cfl=/Users/mauclair/Documents/profiling_preso/hurray.rb
cfn=Object#get_hash
calls=1 27
27 3043
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Enumerable#lazy
calls=1 28
28 4
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Enumerator::Lazy#map
calls=2 28
28 17
events: wall_time
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Float#to_s
0 7509
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Array#map
0 3670
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Float#to_s
calls=10000 31
31 7509
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Integer#to_f
0 1741
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Enumerable#map
0 4
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Range#each
calls=1 28
28 5643
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Range#each
0 7122
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Integer#to_f
calls=10000 29
29 1741
fl=/Users/mauclair/Documents/profiling_preso/a_bit_better.rb
fn=Object#get_hash
20 3
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Range#each
calls=1 22
22 3220
fl=/Users/mauclair/Documents/profiling_preso/a_bit_better.rb
fn=Object#profile_as
8 41
cfl=/Users/mauclair/Documents/profiling_preso/a_bit_better.rb
cfn=Object#get_hash
calls=1 27
27 3223
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Enumerable#map
calls=1 28
28 5647
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Array#map
calls=1 28
28 11179
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
# Action Cable requires that all classes are loaded in advance
Rails.application.eager_load!
run Rails.application
if ENV['PROFILE']
require 'ruby-prof'
use Rack::RubyProf, :path => 'tmp/profile', :printers => [RubyProf::CallTreePrinter], :request_thread_only => true
end
# SNIP
config.cache_classes = !ENV[‘PROFILE’]
# Do not eager load code on boot.
config.eager_load = !ENV[‘PROFILE’]
# SNIP
require 'ruby-prof'
# profile the code
RubyProf.start
# ... code to profile ...
result = RubyProf.stop
printer = RubyProf::CallTreePrinter.new(result)
printer.print(:path => Rails.root.join("tmp"), :profile => "profile")
require 'ruby-prof'
def profile_as(name)
if ENV['PROFILE']
RubyProf.start
yield
result = RubyProf.stop
printer = RubyProf::CallTreePrinter.new(result)
printer.print(:path => '.', :profile => name)
else
yield
end
end
def get_hash
output = {}
(1..10_000).each {|num| output[num] = num*2}
output
end
profile_as('best') do
the_hash = get_hash
(1..10_000).lazy.map do |num|
the_hash[num].to_f
end.map do |num|
num.to_s
end
end
require 'ruby-prof'
def profile_as(name)
if ENV['PROFILE']
RubyProf.start
yield
result = RubyProf.stop
printer = RubyProf::CallTreePrinter.new(result)
printer.print(:path => '.', :profile => name)
else
yield
end
end
def get_hash
output = {}
(1..10_000).each {|num| output[num] = num*2}
output
end
profile_as('real_bad') do
(1..10_000).map do |num|
get_hash[num].to_f
end.map do |num|
num.to_s
end
end
events: wall_time
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Float#to_s
0 5164
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Array#map
0 3526
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Float#to_s
calls=10000 30
30 5164
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Integer#to_f
0 8243
fl=/Users/mauclair/Documents/profiling_preso/oh_no.rb
fn=Object#get_hash
20 18703
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Range#each
calls=10000 22
22 26076196
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Range#each
0 23690
cfl=/Users/mauclair/Documents/profiling_preso/oh_no.rb
cfn=Object#get_hash
calls=10000 28
28 26094899
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Integer#to_f
calls=10000 28
28 8243
fl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
fn=Enumerable#map
0 3
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Range#each
calls=1 27
27 26126832
fl=/Users/mauclair/Documents/profiling_preso/oh_no.rb
fn=Object#profile_as
8 35
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Enumerable#map
calls=1 27
27 26126835
cfl=/Users/mauclair/Documents/profiling_preso/ruby_runtime
cfn=Array#map
calls=1 27
27 8690
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment