Need some help understanding

Hi, i’ve recently started learning Ruby i spent a lot of time reading
and i’ve started working on Test-First Teaching exercise to test what i
have learned. But i got stuck on the performance monitor problem. I cant
seem to understand exactly what it’s asking me to do.

First it asked me to
describe “Performance Monitor” do
before do
@eleven_am = Time.parse(“2011-1-2 11:00:00”)
end

Am i supposed to create a class Performance_monitor and creating an
object variable eleven_am ?
but when i called rake i cant even get pass the “takes about 0 seconds
to run an empty block”. I’ve attempted something simple to test an empty
block but got no result.

def measure(&block)
yield
end

some help on exactly what i’m supposed to be doing would be greatly
appreciated.

Dear Tony L.,

I also “need some help understanding” what you are trying to accomplish.
:wink:

Could you point us the url or copy past the code do we can know what you
are talking about?

Best regards,
Abinoam Jr.
Em 28/02/2014 05:20, “Tony L.” [email protected] escreveu:

He’s talking about
http://testfirst.org/live/learn_ruby/performance_monitor which is,
admittedly, a big leap from the previous test-driven exercises in the
series.

The trick – and it is a trick – is to create a method (not a class)
that calls a block and returns the amount of time that block took to
run. That’s it. All the stuff in the test (including eleven_am) is
there to properly test that that simple method works right. It’s hard
because it’s easy :slight_smile:

  • Alex

Subject: Re: Need some help understanding
Date: sab 01 mar 14 01:52:44 +0100

Quoting Tony L. ([email protected]):

okay so i’ve tried and wrote this method to check how much time passed
when you use a block, now i’m starting to get results.

I cannot understand what you write about your error. But what I am
sure of is that instead of

start_time = Time.at(0)
yield
end_time = Time.at(0)

you should have

start_time=Time.now
yield
end_time=Time.now

Time.at(0) returns a time object pointing to the beginning of Unix’s
time epoch:

p Time::at(0)
1970-01-01 01:00:00 +0100

Carlo

On Sat, Mar 1, 2014 at 1:52 AM, Tony L. [email protected] wrote:

I got this error
takes about 1 second to run a block that sleeps for 1 second (FAILED -
1)

   expected 0.0 to be within 0.1 of 1

What this tells you is that the test was expecting the result to be
between 0.9 and 1.1, and it’s actually 0.0.
As the block is sleeping for one second, it seems that your
calculation of the time is wrong.
Take a look at the method you are using:

it might give you some ideas of what’s wrong.
(Tip: Class: Time (Ruby 2.1.1))

Finished in 1.01 seconds

This proves that the block is really sleeping for 1 second.

i need to reduce the time it finishes…?

You need to calculate correctly how long it takes to run the block.

Jesus.

okay so i’ve tried and wrote this method to check how much time passed
when you use a block, now i’m starting to get results.

def measure()
start_time = Time.at(0)
yield
end_time = Time.at(0)

elapsed_time = start_time - end_time
end

I got this error
takes about 1 second to run a block that sleeps for 1 second (FAILED -
1)

Failures:

  1. Performance Monitor takes about 1 second to run a block that sleeps
    for 1 second
    Failure/Error: elapsed_time.should be_within(0.1).of(1)
    expected 0.0 to be within 0.1 of 1

    ./06_performance_monitor/performance_monitor_spec.rb:37:in `block

(2 levels) in <top (required)>’

Finished in 1.01 seconds
3 examples, 1 failure

i need to reduce the time it finishes…?

oh wait i’m stupid i wrote the math wrong it should be

end_time - start_time

oops! anyway with that out of the way all i had to do was find the
average time for the last part
which is divide the current time by amount entered. just in case anyone
needs help with this in the future the answer is below

require “time”

def measure(repeat = 1)
start_time = Time.now

repeat.times { |x|
yield x
}
end_time = Time.now

elapsed_time = (end_time - start_time)/repeat
end