Skip to content

Instantly share code, notes, and snippets.

@davidpdrsn
Last active June 16, 2017 10:57
Show Gist options
  • Save davidpdrsn/584ba5f4436917ffbddf to your computer and use it in GitHub Desktop.
Save davidpdrsn/584ba5f4436917ffbddf to your computer and use it in GitHub Desktop.
Ruby benchmarking and benchmarking notes

(because I can't remember anything)

Setup

gem install benchmark-ips
gem install stackprof
gem install allocation_tracer

Stackprof

Will answer questions like "which parts of the code are worth optimizing?"

  1. Write something like this

    require "stackprof"
    
    StackProf.run(mode: :cpu, out: "stackprof-cpu-foo.dump") do
      5000.times do
        # code to profile
      end
    end
  2. Run the script

  3. Run stackprof stackprof-cpu-foo.dump --text

Benchmark/ips

Will answer questions like "which of these two things are the fastest, and by how much?"

  1. 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
  2. Run it

Total object allocations

Will answer questions like "how many total allocations does this code make?"

  1. 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
  2. Run it

Whats types are being allocated

Will answer questions like "how many strings does this code allocate?"

  1. 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
  2. Run it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment