New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to benchmark sequentially? #85

Closed
ddnexus opened this Issue Jan 24, 2018 · 6 comments

Comments

Projects
None yet
3 participants
@ddnexus

ddnexus commented Jan 24, 2018

I need to benchmark and compare 2 blocks of code with and without the inclusion of a module.
If I do something like:

def my_method
  Benchmark.ips do |x|

    x.report('without') do
      something
    end

    code_to_include_module

    x.report('with') do
      something
    end

    x.compare!

  end
end

The inclusion happens immediately, so both blocks run with the inclusion. Using hold! doesn't change it. How could I do it?

@danmayer

This comment has been minimized.

Contributor

danmayer commented May 31, 2018

I was struggling to figure out hold! as well but realized you can just call it twice and it will only measure the first block on first run and the second on second run...

def my_method
  Benchmark.ips do |x|

    x.report('without') do
      something
    end

    if ENV['WITH_MODULE']=='true'
      code_to_include_module
    end

    x.report('with') do
      something
    end
    x.hold! 'temp_results'
    x.compare!
  end
end

then run the first time like so ruby yourfile.rb which outputs

Warming up --------------------------------------
           without     1.000  i/100ms
Calculating -------------------------------------
           without      3.650  (± 0.0%) i/s -     19.000  in   5.248510s

Pausing here -- run Ruby again to measure the next benchmark...

and on the second run like so WITH_MODULE=true ruby yourfile.rb which outputs

Warming up --------------------------------------
        with     1.000  i/100ms
Calculating -------------------------------------
        with   14.200  (±14.1%) i/s -     70.000  in   5.024695s

Comparison:
        without:       14.2 i/s
             with:        3.6 i/s - 3.89x  slower

hope that helps @ddnexus

@ddnexus

This comment has been minimized.

ddnexus commented May 31, 2018

Thank you! It helps a lot!

@danmayer

This comment has been minimized.

Contributor

danmayer commented Jun 1, 2018

ok added full example as a PR @ddnexus #86

@kbrock

This comment has been minimized.

Contributor

kbrock commented Oct 19, 2018

can this issue be closed? The example is great and seems to answer this question

@danmayer

This comment has been minimized.

Contributor

danmayer commented Oct 22, 2018

yes it was all merged in, so I would +1 closing this issue, I think @evanphx would need to do that ;)

@ddnexus

This comment has been minimized.

ddnexus commented Oct 22, 2018

Sorry for the late answer. Yes I can close the issue. THank you!

@ddnexus ddnexus closed this Oct 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment