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


duerst (Martin Dürst) wrote:
> It's going to be very confusing to have different methods on Time use different units. In some years, nobody will remember which method is new and which is old.

You're right.  It makes sense to exactly mimic `time - other_time`, and return the number of seconds in a float.

nobu (Nobuyoshi Nakada) wrote:
> phluid61 (Matthew Kerwin) wrote:
> 
> > 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)
> 
> time_obj.elapsed(in: :nanoseconds) ?

I had considered that, but I wonder if people aren't going to ask for (or expect) support for :milliseconds, :hours, :days, :aztec_great_cycles, etc.  The rules about which symbols are valid seems a little arbitrary.  And then I suppose the question could become: why are nanoseconds an integer, but seconds a float?  And why doesn't Time#- accept the same kwarg?  And so on with the bike-shedding.  I don't know a good answer for most of these hypothetical questions, so I guess the short-term solution would be to make #elapsed be a short-cut for `Time.now - self`, and return a float number of seconds.

Also, I feel obliged to mention that this feature is related to #8096, FWIW.
----------------------------------------
Feature #8640: Add Time#elapsed to return nanoseconds since creation
https://bugs.ruby-lang.org/issues/8640#change-40531

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/