module Benchmark::IPS

Benchmark in iterations per second, no more guessing!

See Benchmark.ips for documentation on using this gem~

@see {github.com/evanphx/benchmark-ips}

Constants

CODENAME

CODENAME of current version.

VERSION

Benchmark-ips Gem version.

Public Class Methods

options() click to toggle source

Set options for running the benchmarks. :format => [:human, :raw]

:human format narrows precision and scales results for readability
:raw format displays 6 places of precision and exact iteration counts
# File lib/benchmark/ips.rb, line 109
def self.options
  @options ||= {:format => :human}
end

Public Instance Methods

ips(*args) { |job| ... } click to toggle source

Measure code in block, each code’s benchmarked result will display in iteration per second with standard deviation in given time. @param time [Integer] Specify how long should benchmark your code in seconds. @param warmup [Integer] Specify how long should Warmup time run in seconds. @return [Report]

# File lib/benchmark/ips.rb, line 33
def ips(*args)
  if args[0].is_a?(Hash)
    time, warmup, quiet = args[0].values_at(:time, :warmup, :quiet)
  else
    time, warmup, quiet = args
  end

  sync, $stdout.sync = $stdout.sync, true

  job = Job.new

  job_opts = {}
  job_opts[:time] = time unless time.nil?
  job_opts[:warmup] = warmup unless warmup.nil?
  job_opts[:quiet] = quiet unless quiet.nil?

  job.config job_opts

  yield job

  job.load_held_results

  job.run

  if job.run_single? && job.all_results_have_been_run?
    job.clear_held_results
  else
    job.save_held_results
    puts '', 'Pausing here -- run Ruby again to measure the next benchmark...' if job.run_single?
  end

  $stdout.sync = sync
  job.run_comparison
  job.generate_json

  report = job.full_report

  if ENV['SHARE'] || ENV['SHARE_URL']
    require 'benchmark/ips/share'
    share = Share.new report, job
    share.share
  end

  report
end
ips_quick(*methods, on: Kernel, **opts) click to toggle source

Quickly compare multiple methods on the same object. @param methods [Symbol…] A list of method names (as symbols) to compare. @param receiver [Object] The object on which to call the methods. Defaults to Kernel. @param opts [Hash] Additional options for customizing the benchmark. @option opts [Integer] :warmup The number of seconds to warm up the benchmark. @option opts [Integer] :time The number of seconds to run the benchmark.

@example Compare String#upcase and String#downcase

ips_quick(:upcase, :downcase, on: "hello")

@example Compare two methods you just defined, with a custom warmup.

def add; 1+1; end
def sub; 2-1; end
ips_quick(:add, :sub, warmup: 10)
# File lib/benchmark/ips.rb, line 93
def ips_quick(*methods, on: Kernel, **opts)
  ips(opts) do |x|
    x.compare!

    methods.each do |name|
      x.report(name) do |iter|
        iter.times { on.__send__ name }
      end
    end
  end
end