From: "phluid61 (Matthew Kerwin)" Date: 2013-07-19T08:00:02+09:00 Subject: [ruby-core:56082] [CommonRuby - Feature #8640] Add Time#elapsed to return nanoseconds since creation Issue #8640 has been updated by phluid61 (Matthew Kerwin). rosenfeld (Rodrigo Rosenfeld Rosas) wrote: > Em 18-07-2013 03:03, Aaron Patterson escreveu: > >> In that case, would #8096 be a better proposal? Since that one doesn't even allocate a Time object. > > I don't think so. We need subsecond resolution, which (if we used > > #8096) would require a possible bignum allocation (from what I gather in > > the ticket). > > But I believe he has a point, Aaron. Maybe we should avoid allocating a > time object (Time.now) and instead introduce something like Java's > System.currentTimeMillis(): > > start = Benchmark.start > operation > report start.since_beginning > other_operation > report start.since_beginning # from start= on > report start.since_last_report # since operation > > This new object would be lighter than Time as it would only contain the > start absolute nanoseconds or millis since epoch, maybe. > > I haven't given the names much of a thought, but that would be the idea. > Makes sense? Actually the supplied patch uses clock_gettime(CLOCK_REALTIME), which is already the C equivalent of System.currentTimeMillis(). The only issue is that it allocates a Time object (although it doesn't matter because that happens before the benchmarking commences). Since the getting of the final time and the calculation of the elapsed duration all happen in C-land, there's very little cost involved, even when the numbers are very large. However, one of the threads of discussion on #8096 suggested using clock_gettime(CLOCK_MONOTONIC)* or System.nanoTime(), which count from an arbitrary epoch (so the number would usually be smaller and thus more likely to fit into a Fixnum). It would be quite light to do: start = Time.timestamp # clock_gettime(CLOCK_MONOTONIC) => Fixnum # ... stuff ... delta = start - Time.timestamp # => Fixnum ..even if you have to do more of the logic yourself. I'm +0 for the original proposal, just because of the naming/kwarg issue, otherwise I'd be +1. * or CLOCK_PROCESS_CPUTIME_ID or CLOCK_THREAD_CPUTIME_ID if they're available, and that's what you need. ---------------------------------------- Feature #8640: Add Time#elapsed to return nanoseconds since creation https://bugs.ruby-lang.org/issues/8640#change-40580 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/