469,270 Members | 1,038 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

How to get Time difference from system time?

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
Feb 12 '08 #1
2 8628
improvcornartist
303 Expert 100+
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)
Feb 12 '08 #2
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 :)
Feb 13 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by newbiecpp | last post: by
3 posts views Thread by Jon Davis | last post: by
3 posts views Thread by Randall Parker | last post: by
3 posts views Thread by moni | last post: by
18 posts views Thread by moni | last post: by
1 post views Thread by indiarocks | last post: by
3 posts views Thread by bbawa1 | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.