edymerchk
edymerchk

Reputation: 1263

Measuring time in ruby

I need to measure the time of execution of a java class, when I used a script file with this and run "ruby file.rb" from the terminal works perfect always, and I always get values around 10 seconds:

ini = Time.now
`java file.class`
fin = Time.now
puts "#{fin-ini}"

but when I do the exactly same in a controller of a rails application always I get values like 5.626041666666666e-10

anyone knows what is happening

Upvotes: 1

Views: 282

Answers (1)

sunnyrjuneja
sunnyrjuneja

Reputation: 6123

Most calculators and many computer programs present very large and very small results in scientific notation. Because superscripted exponents like 107 cannot always be conveniently displayed, the letter E or e is often used to represent times ten raised to the power of (which would be written as "x 10b") and is followed by the value of the exponent.

From: http://en.wikipedia.org/wiki/Scientific_notation#E_notation

There is a VERY small time between when ini is initialized and fin is and what you're seeing is the difference between the two.

All it really means is 5.626041666666666×10^(-10) or 0.0000000005626041666666666 seconds or milliseconds or whatever your unit of measure is.

If you're interested in rounding this, you can always do #{(fin - ini).to_i} which will convert it to an integer, 0.

Upvotes: 2

Related Questions