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.

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".
some help on exactly what i'm supposed to be doing would be greatly
appreciated.

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Fri, Feb 28, 2014 at 5:18 AM, Abinoam Jr. <abinoam@gmail.com> wrote:

Dear Tony Lee,

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 Lee" <lists@ruby-forum.com> escreveu:

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.

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".
some help on exactly what i'm supposed to be doing would be greatly
appreciated.

--
Posted via http://www.ruby-forum.com/\.

--
Alex Chaffee - alex@stinky.com
http://alexchaffee.com
http://codelikethis.com
http://twitter.com/alexch

Dear Tony Lee,

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 Lee" <lists@ruby-forum.com> escreveu:

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.

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".
some help on exactly what i'm supposed to be doing would be greatly
appreciated.

--
Posted via http://www.ruby-forum.com/\.

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..?

···

--
Posted via http://www.ruby-forum.com/.

oh wait i'm stupid i did 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

···

--
Posted via http://www.ruby-forum.com/.

Quoting Tony Lee (lists@ruby-forum.com):

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

···

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

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

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)

       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.

···

On Sat, Mar 1, 2014 at 1:52 AM, Tony Lee <lists@ruby-forum.com> wrote: