-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Replace System.currentTimeMillis() by System.nanoTime() #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,24 +23,20 @@ public class Profiler { | |
| private Long startTickInMs; | ||
| private Long stopTickInMs; | ||
|
|
||
| public Profiler() { | ||
| startTickInMs = null; | ||
| stopTickInMs = null; | ||
| } | ||
|
|
||
| public long start() { | ||
| startTickInMs = System.currentTimeMillis(); | ||
| startTickInMs = System.nanoTime(); | ||
| return startTickInMs.longValue(); | ||
| } | ||
|
|
||
| public long stop() { | ||
| stopTickInMs = System.currentTimeMillis(); | ||
| stopTickInMs = System.nanoTime(); | ||
| return stopTickInMs.longValue(); | ||
| } | ||
|
|
||
| public long getDuration() { | ||
| if (startTickInMs != null && stopTickInMs != null) | ||
| if (startTickInMs != null && stopTickInMs != null) { | ||
| return stopTickInMs.longValue() - startTickInMs.longValue(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hasn't the semantics changed as it no longer return milliseconds but now micro-(or even nano-) seconds? (this goes for stop and start as well btw. Did you check the callers?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did... and it doesn't change. Only the resolution does, Quick googled that one here for you: https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks Implementation is still the same: http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime Cheers,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, and so does the semantics of the return of starts() stop() and getDuration(). |
||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
@@ -55,12 +51,14 @@ public boolean isStopped() { | |
|
|
||
| @Override | ||
| public String toString() { | ||
| if (startTickInMs == null) | ||
| if (startTickInMs == null) { | ||
| return "Not Started"; | ||
| } | ||
|
|
||
| if (stopTickInMs == null) | ||
| if (stopTickInMs == null) { | ||
| return "Started but not stopped"; | ||
| } | ||
|
|
||
| return "Done. Duration: " + getDuration() + "ms"; | ||
| } | ||
| } | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use a new name for the var, like startTickInNanos (and of course stopTickInNanos)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The nanoTime() is given in Microseconds. So, the MS still fits in.
Cheers,
Wilder