Forum: Ruby basic millisecond code profile?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aaron S. (Guest)
on 2007-06-26 00:20
if I need to see the amount of milliseconds / seconds a method takes can
I do something like this:

class Person
  attr_accessor :first
  def pl
    now = Time.now.usec #get milliseconds
    0.upto(10000) do |i|
      i = 'g'
    end
    now2 = Time.now.usec #get milliseconds
    puts String(now2 - now)
  end
end
p = Person.new
p.pl

whats the best way to do this?
Aaron S. (Guest)
on 2007-06-26 00:20
Aaron S. wrote:
> if I need to see the amount of milliseconds / seconds a method takes can
> I do something like this:
>
> class Person
>   attr_accessor :first
>   def pl
>     now = Time.now.usec #get milliseconds
>     0.upto(10000) do |i|
>       i = 'g'
>     end
>     now2 = Time.now.usec #get milliseconds
>     puts String(now2 - now)
>   end
> end
> p = Person.new
> p.pl
>
> whats the best way to do this?

class Person
  attr_accessor :first
  def pl
    now = Time.now.usec / 1000 #get milliseconds
    0.upto(10000) do |i|
      i = 'g'
    end
    now2 = Time.now.usec / 1000 #get milliseconds
    puts String(now2 - now)
  end
end

p = Person.new
p.pl

whoops, forgot the /1000
This topic is locked and can not be replied to.