nanotime

Is System.nanoTime() consistent across threads?

我的梦境 提交于 2019-11-29 09:31:12
I want to count the time elapsed between two events in nanoseconds. To do that, I can use System.nanoTime() as mentioned here . The problem is that the two events are happening in different threads. Since nanoTime() doesn't return an absolute timestamp but instead can only be used to calculate time differences , I'd like to know if the values I get on the two different threads are consistent with the physical time elapsed between the two events. It's supposed to be , but due to buggy kernels or hardware, the answer can be no , at least in some environments. 来源: https://stackoverflow.com

java.sql.Timestamp way of storing NanoSeconds

不想你离开。 提交于 2019-11-29 07:04:20
java.sql.Timestamp constructor go like this: public Timestamp(long time) { super((time/1000)*1000); nanos = (int)((time%1000) * 1000000); if (nanos < 0) { nanos = 1000000000 + nanos; super.setTime(((time/1000)-1)*1000); } } It basically accepts time in millisecond and then extracts the last 3 digits and makes it nanos. So for a millisecond value of 1304135631 421 , I'm getting Timestamp.getnanos() as 421000000 . This is plain calculation (adding 6 zeroes at the end)... does not seems to be optimum. A better way could have been Timestamp constructor that accepts time in nanoseconds and then

Why I get a negative elapsed time using System.nanoTime()?

[亡魂溺海] 提交于 2019-11-29 03:30:19
I'm trying to use following code with System.nanoTime() to measure the elapsed time of code. public static void main(String[] args) throws Exception { while (true) { long start = System.nanoTime(); for (int i = 0; i < 10000; i++) ; long end = System.nanoTime(); long cost = end - start; if (cost < 0) { System.out.println("start: " + start + ", end: " + end + ", cost: " + cost); } } } And I get such result: start: 34571588742886, end: 34571585695366, cost: -3047520 start: 34571590239323, end: 34571586847711, cost: -3391612 start: 34571651240343, end: 34571648928369, cost: -2311974 start:

How to suspend a java thread for a small period of time, like 100 nanoseconds?

大城市里の小女人 提交于 2019-11-28 08:57:53
I know Thread.sleep() can make a java thread suspend for a while, like certain milliseconds and certain nanoseconds. But the problem is the invocation of this function also causes overhead. For example, if I want a thread to suspend for 100 nanoseconds, and I call Thread.sleep(0, 100) . The whole cost for this process is invocation_cost + 100 nanosceonds , which may be much larger the what I want. How could I avoid this problem, and achieve my purpose? The reason I need this is that I want to do simulation offline. I profiled the execution time of a task; Now I want to simulate this execution

Is System.nanoTime() consistent across threads?

倖福魔咒の 提交于 2019-11-28 03:01:41
问题 I want to count the time elapsed between two events in nanoseconds. To do that, I can use System.nanoTime() as mentioned here. The problem is that the two events are happening in different threads. Since nanoTime() doesn't return an absolute timestamp but instead can only be used to calculate time differences , I'd like to know if the values I get on the two different threads are consistent with the physical time elapsed between the two events. 回答1: It's supposed to be, but due to buggy

java.sql.Timestamp way of storing NanoSeconds

送分小仙女□ 提交于 2019-11-28 00:53:16
问题 java.sql.Timestamp constructor go like this: public Timestamp(long time) { super((time/1000)*1000); nanos = (int)((time%1000) * 1000000); if (nanos < 0) { nanos = 1000000000 + nanos; super.setTime(((time/1000)-1)*1000); } } It basically accepts time in millisecond and then extracts the last 3 digits and makes it nanos. So for a millisecond value of 1304135631 421 , I'm getting Timestamp.getnanos() as 421000000 . This is plain calculation (adding 6 zeroes at the end)... does not seems to be

Why I get a negative elapsed time using System.nanoTime()?

烂漫一生 提交于 2019-11-27 17:34:17
问题 I'm trying to use following code with System.nanoTime() to measure the elapsed time of code. public static void main(String[] args) throws Exception { while (true) { long start = System.nanoTime(); for (int i = 0; i < 10000; i++) ; long end = System.nanoTime(); long cost = end - start; if (cost < 0) { System.out.println("start: " + start + ", end: " + end + ", cost: " + cost); } } } And I get such result: start: 34571588742886, end: 34571585695366, cost: -3047520 start: 34571590239323, end:

Precision vs. accuracy of System.nanoTime()

[亡魂溺海] 提交于 2019-11-27 04:47:27
The documentation for System.nanoTime() says the following (emphasis mine). This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. As I see it, this can be interpreted in two different ways: The sentence in bold above refers to individual return values. Then,

Precision vs. accuracy of System.nanoTime()

别等时光非礼了梦想. 提交于 2019-11-26 22:15:03
问题 The documentation for System.nanoTime() says the following (emphasis mine). This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. As I see it, this can be

Java 8 Instant.now() with nanosecond resolution?

我的未来我决定 提交于 2019-11-26 20:36:59
Java 8's java.time.Instant stores in "nanosecond resolution", but using Instant.now() only provides millisecond resolution... Instant instant = Instant.now(); System.out.println(instant); System.out.println(instant.getNano()); Result... 2013-12-19T18:22:39.639Z 639000000 How can I get an Instant whose value is 'now', but with nanosecond resolution? While default Java8 clock does not provide nanoseconds resolution, you can combine it with Java ability to measure time differences with nanoseconds resolution, thus creating an actual nanosecond-capable clock. public class NanoClock extends Clock {