Issue #8640 has been updated by phluid61 (Matthew Kerwin).


+1

duerst (Martin Dürst) wrote:
> I think this is a good idea. But since
> duration = Time.now - start
> is in seconds, I'd either keep seconds as units, or make the use of nanoseconds explicit, e.g. like this:
> duration = start.elapsed_nanoseconds

Since this is a new feature I think we don't need to worry as much about existing functionality.  Personally I prefer the function to return a higher-precision integer over a float.

I wouldn't complain if there was a kwarg, although I'm not sure which way it should switch:  time_obj.elapsed(float: true) or time_obj.elapsed(nanoseconds: true)
----------------------------------------
Feature #8640: Add Time#elapsed to return nanoseconds since creation
https://bugs.ruby-lang.org/issues/8640#change-40525

Author: tenderlovemaking (Aaron Patterson)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


Hi,

We time many things in Rails (and so does lots of other code), and I've found that we spend lots of time allocating and subtracting time objects.  For example:

start = Time.now
##  do stuff we want to time
finish = Time.now - start

It would be nice if we could just create one time object and grab the nanoseconds elapsed since the time object was created like so:

start = Time.now
## do stuff we want to time
finished = start.elapsed # => nanoseconds elapsed.

I've attached a patch that implements this feature.


-- 
http://bugs.ruby-lang.org/