(because I can't remember anything)
gem install benchmark-ips
gem install stackprof
gem install allocation_tracer
Will answer questions like "which parts of the code are worth optimizing?"
-
Write something like this
require "stackprof" StackProf.run(mode: :cpu, out: "stackprof-cpu-foo.dump") do 5000.times do # code to profile end end
-
Run the script
-
Run
stackprof stackprof-cpu-foo.dump --text
Will answer questions like "which of these two things are the fastest, and by how much?"
-
Write something like this
require "benchmark/ips" Benchmark.ips do |x| x.report("single") do 'hi there' end x.report("double") do "hi there" end x.compare! end
-
Run it
Will answer questions like "how many total allocations does this code make?"
-
Write something like this
def objects_allocated_by(iterations: 5000) yield before = GC.stat(:total_allocated_object) iterations.times { yield } after = GC.stat(:total_allocated_object) (after - before) / iterations end delta = objects_allocated_by do # some code end p delta
-
Run it
Will answer questions like "how many strings does this code allocate?"
- Write something like this
require 'allocation_tracer' require 'pp' ObjectSpace::AllocationTracer.setup(%i{type}) result = ObjectSpace::AllocationTracer.trace do 5000.times{|i| ["a", 1, {}] } end pp result
- Run it