Guava: Stopwatch: add getStartTime()

Created on 2 Mar 2017  Â·  8Comments  Â·  Source: google/guava

Stopwatch is missing a getter for the start time.
If you're happy with the API change, I can submit a PR.

Cheers

Most helpful comment

Sorry, my bad. I now understand that elapsed(Timeunit) returns the elapsed time since the stopwatch was started.
So what I'm missing is a method (let's call it "partialElapsed") to get the elapsed time between now and the previous call to elapsed (or since started), so that I can write:

sw.start();
do1();
log.debug("do1 took " + sw.partialElapsed(MILLIS) + "ms);
..
doN();
log.debug("doN took " + sw.partialElapsed(MILLIS) + "ms);
log.debug("The whole process took " + sw.elapsed(MILLIS) + "ms);

For example, the stopwatch included in the Google app Clock on Android 6 supports this.

PS: @jbduncan Thanks for the link, funny and useful video :)

All 8 comments

I'm not sure what you're defining as "the start time"? Stopwatch's whole point is that it only lets you get the difference between times. (And on the backend it's getting System.nanoTime() which has no independent meaning anyway...?)

Right. Better put, it's missing a feature to get the elapsed time since it was started.

I'm not sure what the distinction is between what you're looking for and Stopwatch.elapsed(TimeUnit).

Please correct me if I'm wrong @alb-i986, but if starting time is something you're after, then I think you might be better served by something like or based on Java 8's new date library or joda-time.

After all, if the amount of trouble programmers go through just to program timezones are any indication, then I would suspect that finding the time elapsed between a "starting point" and some point in the future or past (which I assume is what you want to do) is probably harder than one would think. :/

Sorry, my bad. I now understand that elapsed(Timeunit) returns the elapsed time since the stopwatch was started.
So what I'm missing is a method (let's call it "partialElapsed") to get the elapsed time between now and the previous call to elapsed (or since started), so that I can write:

sw.start();
do1();
log.debug("do1 took " + sw.partialElapsed(MILLIS) + "ms);
..
doN();
log.debug("doN took " + sw.partialElapsed(MILLIS) + "ms);
log.debug("The whole process took " + sw.elapsed(MILLIS) + "ms);

For example, the stopwatch included in the Google app Clock on Android 6 supports this.

PS: @jbduncan Thanks for the link, funny and useful video :)

Just use two Stopwatches, resetting one each time you read it?

On Fri, Mar 3, 2017 at 5:35 AM, Alberto Scotto notifications@github.com
wrote:

Sorry, my bad. I now understand that elapsed(Timeunit) returns the
elapsed time since the stopwatch was started.
So what I'm missing is a method (let's call it "partialElapsed") to get
the elapsed time between now and the previous call to elapsed, so that I
can write:

sw.start();
do1();
log.debug("do1 took " + sw.partialElapsed(MILLIS) + "ms);
..
doN();
log.debug("doN took " + sw.elapsedSincePrevious(MILLIS) + "ms);
log.debug("The whole process took " + sw.elapsed(MILLIS) + "ms);

For example, the stopwatch included in the Google app Clock on Android 6
supports this.

PS: @jbduncan https://github.com/jbduncan Thanks for the link, funny
and useful video :)

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/google/guava/issues/2752#issuecomment-283954031, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AA5Clx5b97rUV6ZrtXiP4EmIyHVXBiAFks5riBc5gaJpZM4MRgYJ
.

--
Kevin Bourrillion | Java Librarian | Google, Inc. | [email protected]

@kevinb9n that's not even necessary, you can achieve this result fairly easily using the existing Stopwatch API.

Stopwatch stopwatch = Stopwatch.createStarted();

benchmark();

System.out.printf("benchmark() took %d ms.%n", stopwatch.elapsed(TimeUnit.MILLISECONDS));
stopwatch.reset().start();

benchmark();

System.out.printf("benchmark() took %d ms.%n", stopwatch.elapsed(TimeUnit.MILLISECONDS));
stopwatch.reset().start();

...

He also wants the total time.

On Fri, Mar 3, 2017 at 9:03 AM, Ryley Kimmel notifications@github.com
wrote:

@kevinb9n https://github.com/kevinb9n that's not even necessary, you
can achieve this result fairly easily using the existing Stopwatch API.

Stopwatch stopwatch = Stopwatch.createStarted();

benchmark();
System.out.printf("benchmark() took %d ms.%n", stopwatch.elapsed(TimeUnit.MILLISECONDS));
stopwatch.reset().start();

benchmark();
System.out.printf("benchmark() took %d ms.%n", stopwatch.elapsed(TimeUnit.MILLISECONDS));
stopwatch.reset().start();
...

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/google/guava/issues/2752#issuecomment-284010571, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AA5Cl-Dkigbd8CpHdNpzShShe6teNhNaks5riEf_gaJpZM4MRgYJ
.

--
Kevin Bourrillion | Java Librarian | Google, Inc. | [email protected]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

terence1984 picture terence1984  Â·  3Comments

Hanmac picture Hanmac  Â·  3Comments

philgebhardt picture philgebhardt  Â·  3Comments

gissuebot picture gissuebot  Â·  5Comments

cpovirk picture cpovirk  Â·  5Comments