How to calculate the difference between two distinct hours in milliseconds?

1

What is the way to calculate in milliseconds the time of an action in ruby?

I'm doing the following:

start_time = Time.now
Execute given code that takes a few milliseconds
end_time = (Time.now - start_time)

And as a result I get for example: 0.048813

The question is: Is this value in milliseconds (ms) or in seconds?

How do I check it with fewer decimal places, type 48.81 ms?

    
asked by anonymous 18.02.2014 / 21:38

1 answer

1

According to the documentation , the subtraction between two times returns a number of seconds. So you can get the value in milliseconds like this:

delta_time = (Time.now - start_time) * 1000

To display with only two decimal places you can use sprintf . This way:

puts "%.2f" % delta_time
    
18.02.2014 / 21:45