Connecting Tech Pros Worldwide Forums | Help | Site Map

How Get Time difference from system time.

Newbie
 
Join Date: Jan 2008
Posts: 17
#1: Feb 12 '08
I am using the following to get the time:

puts Time.now --> starting of my script

<MY CODE>

puts Time.now --> end of my script

I want to get the Time difference between two Time.now
Problem I am facing is i cannot directly take the difference as Time.now displays the time in following format : "Tue Feb 12 19:55:37 +0530 2008"
Any other better way to solve it?

Cheers

Expert
 
Join Date: May 2007
Posts: 213
#2: Feb 12 '08

re: How Get Time difference from system time.


You can subtract the times to get the number of seconds between. Then you will have to do some manipulation to get an output in a format you want. For instance, to determine the number of minutes and seconds a piece of code runs, you could use
Expand|Select|Wrap|Line Numbers
  1. @start = Time.now
  2. #Execute code
  3. @run_time = Time.now - @start
  4. p sprintf("%d min ", @run_time / 60) + sprintf("%0.2f sec", @run_time % 60)
Newbie
 
Join Date: Jan 2008
Posts: 17
#3: Feb 13 '08

re: How Get Time difference from system time.


Quote:

Originally Posted by improvcornartist

You can subtract the times to get the number of seconds between. Then you will have to do some manipulation to get an output in a format you want. For instance, to determine the number of minutes and seconds a piece of code runs, you could use

Expand|Select|Wrap|Line Numbers
  1. @start = Time.now
  2. #Execute code
  3. @run_time = Time.now - @start
  4. p sprintf("%d min ", @run_time / 60) + sprintf("%0.2f sec", @run_time % 60)

Thanks Alot, works Fine :)
Reply