Skip to content

Commit

Permalink
Splat methods, send all options to Job
Browse files Browse the repository at this point in the history
  • Loading branch information
nateberkopec committed Aug 29, 2024
1 parent 09a2918 commit 647458b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ guessing iteration counts as we do with the traditional Benchmark library.
You can also use `ips_quick` to save a few lines of code:

```ruby
Benchmark.ips_quick([:upcase, :downcase], "hello") # runs a suite comparing "hello".upcase and "hello".downcase
Benchmark.ips_quick(:upcase, :downcase, on: "hello") # runs a suite comparing "hello".upcase and "hello".downcase

def first; MyJob.perform(1); end
def second; MyJobOptimized.perform(1); end
Benchmark.ips_quick([:first, second]) # compares :first and :second
Benchmark.ips_quick(:first, :second) # compares :first and :second
```

This adds a very small amount of overhead, which may be significant (i.e. ips_quick will understate the difference) if you're microbenchmarking things that can do over 1 million iterations per second. In that case, you're better off using the full format.
Expand Down
4 changes: 2 additions & 2 deletions examples/quick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def sub
2 - 1
end

Benchmark.ips_quick([:add, :sub], warmup: 1, time: 1)
Benchmark.ips_quick(:add, :sub, warmup: 1, time: 1)

h = {}

Benchmark.ips_quick([:size, :empty?], h)
Benchmark.ips_quick(:size, :empty?, on: h)
19 changes: 9 additions & 10 deletions lib/benchmark/ips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,27 @@ def ips(*args)
end

# Quickly compare multiple methods on the same object.
# @param methods [Array<Symbol>] An array of method names (as symbols) to compare.
# @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. (optional)
# @option opts [Integer] :time The number of seconds to run the benchmark. (optional)
# @param opts [Hash] Additional options for customizing the benchmark. See Job.config for all available options.
# @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], "hello")
# 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)
def ips_quick(methods, receiver = Kernel, **opts)
# ips_quick(:add, :sub, warmup: 10)
def ips_quick(*methods, on: Kernel, **opts)
ips do |x|
x.compare!
x.warmup = opts[:warmup] if opts[:warmup]
x.time = opts[:time] if opts[:time]
x.config(opts) if opts

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

0 comments on commit 647458b

Please sign in to comment.