Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was searching for a quick and dirty stop watch class to avoid coding such and came upon the site where the original code was posted and this site as well.</p> <p>In the end, I modified the code until it met what I think that I was originally searching for.</p> <p>In case anyone is interested, the version that I have ended up thus far with is as follows (albeit that I have yet to apply it in the application that I am currently updating and for which I want to make use of such functionality).</p> <pre><code># REFERENCES # 1. http://stackoverflow.com/questions/858970/how-to-get-a-stopwatch-program-running # 2. http://codeidol.com/other/rubyckbk/User-Interface/Creating-a-GUI-Application-with-Tk/ # 3. http://books.google.com.au/books?id=bJkznhZBG6gC&amp;pg=PA806&amp;lpg=PA806&amp;dq=ruby+stopwatch+class&amp;source=bl&amp;ots=AlH2e7oWWJ&amp;sig=KLFR-qvNfBfD8WMrUEbVqMbN_4o&amp;hl=en&amp;ei=WRjOTbbNNo2-uwOkiZGwCg&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=8&amp;ved=0CEsQ6AEwBw#v=onepage&amp;q=ruby%20stopwatch%20class&amp;f=false # 4. http://4loc.wordpress.com/2008/09/24/formatting-dates-and-floats-in-ruby/ module Utilities class StopWatch def new() @watch_start_time = nil #Time (in seconds) when the stop watch was started (i.e. the start() method was called). @lap_start_time = nil #Time (in seconds) when the current lap started. end #def new def start() myCurrentTime = Time.now() #Current time in (fractional) seconds since the Epoch (January 1, 1970 00:00 UTC) if (!running?) then @watch_start_time = myCurrentTime @lap_start_time = @watch_start_time end #if myCurrentTime - @watch_start_time; end #def start def lap_time_seconds() myCurrentTime = Time.now() myLapTimeSeconds = myCurrentTime - @lap_start_time @lap_start_time = myCurrentTime myLapTimeSeconds end #def lap_time_seconds def stop() myTotalSecondsElapsed = Time.now() - @watch_start_time @watch_start_time = nil myTotalSecondsElapsed end #def stop def running?() !@watch_start_time.nil? end #def end #class StopWatch end #module Utilities def kill_time(aRepeatCount) aRepeatCount.times do #just killing time end #do end #def kill_time elapsed_time_format_string = '%.3f' myStopWatch = Utilities::StopWatch.new() puts 'total time elapsed: ' + elapsed_time_format_string % myStopWatch.start() + ' seconds' kill_time(10000000) puts 'lap time: ' + elapsed_time_format_string % myStopWatch.lap_time_seconds() + ' seconds' kill_time(20000000) puts 'lap time: ' + elapsed_time_format_string % myStopWatch.lap_time_seconds() + ' seconds' kill_time(30000000) puts 'lap time: ' + elapsed_time_format_string % myStopWatch.lap_time_seconds() + ' seconds' puts 'total time elapsed: ' + elapsed_time_format_string % myStopWatch.stop() + ' seconds' </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload