Questions tagged [nanotime]

Anything related to programming languages functions (or other similar facilities) allowing the retrieval of the current time with a resolution in the nanoseconds range.

Anything related to programming languages functions (or other similar facilities) allowing the retrieval of the current time with a resolution in the nanoseconds (ns) range.

136 questions
157
votes
14 answers

Is System.nanoTime() completely useless?

As documented in the blog post Beware of System.nanoTime() in Java, on x86 systems, Java's System.nanoTime() returns the time value using a CPU specific counter. Now consider the following case I use to measure time of a call: long time1=…
pdeva
  • 36,445
  • 42
  • 122
  • 154
30
votes
6 answers

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

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…
JackWM
  • 8,835
  • 18
  • 58
  • 90
29
votes
4 answers

Conversion of nanoseconds to milliseconds and nanoseconds < 999999 in Java

I'm wondering what the most accurate way of converting a big nanoseconds value is to milliseconds and nanoseconds, with an upper limit on the nanoseconds of 999999. The goal is to combine the nanoseconds and milliseconds values to ensure the maximum…
Chris Dennett
  • 21,465
  • 8
  • 52
  • 84
28
votes
3 answers

How to get time in PHP with nanosecond precision?

Is this even possible in PHP? If not, what is the highest precision available?
Matt
  • 10,197
  • 24
  • 77
  • 109
24
votes
5 answers

Java 8 Instant.now() with nanosecond resolution?

Java 8's java.time.Instant stores in "nanosecond resolution", but using Instant.now() only provides millisecond resolution... Instant instant =…
user1615355
  • 241
  • 1
  • 2
  • 3
24
votes
4 answers

Precision vs. accuracy of System.nanoTime()

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…
andreasdr
  • 3,172
  • 4
  • 21
  • 29
19
votes
2 answers

How to convert epoch time with nanoseconds to human-readable?

I have a timestamp in epoch time with nanoseconds - e.g. 1360287003083988472 nanoseconds since 1970-01-01. The Python datetime objects and conversion methods only support up to millisecond precision. Is there an easy way to convert this epoch time…
victorhooi
  • 14,225
  • 20
  • 79
  • 112
19
votes
7 answers

What is the equivalent to System.nanoTime() in .NET?

The title is pretty much self-explanatory, I'm killing myself over this simplicity. Looked here, but it isn't much helpful.
Lazlo
  • 7,888
  • 13
  • 69
  • 113
15
votes
1 answer

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

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;…
Freewind
  • 177,284
  • 143
  • 381
  • 649
15
votes
3 answers

SensorEvent.timestamp inconsistency

my application performs in background step counting using the step detector sensor API's introduced in android 4.4.X. It's essential to my app to know the exact time (at least accuracy of a second) each step event has accrued. because I perform…
Tal Kanel
  • 9,855
  • 10
  • 55
  • 93
14
votes
3 answers

Why is there such a big difference on time between first nanoTime() call and the successive calls?

So my question is more general. I've the following simple code: for(int i=0;i<10;i++){ long starttime=System.nanoTime(); System.out.println("test"); long runtime=System.nanoTime()-starttime; System.out.println(i + ":"…
Jürgen K.
  • 2,889
  • 4
  • 24
  • 52
13
votes
6 answers

How to get a meaningful result from subtracting 2 nanoTime objects?

I created a filter that monitors the length of a request. long start = System.nanoTime(); ... long end = System.nanoTime(); How can I get the number of milliseconds from this now?
Blankman
  • 236,778
  • 296
  • 715
  • 1,125
13
votes
1 answer

Is System.nanoTime() consistent across threads?

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…
agentofuser
  • 8,291
  • 10
  • 48
  • 81
11
votes
4 answers

Why is my System.nanoTime() broken?

Myself and another developer on my time recently moved from a Core 2 Duo machine at work to a new Core 2 Quad 9505; both running Windows XP SP3 32-bit with JDK 1.6.0_18. Upon doing so, a couple of our automated unit tests for some…
Chad
  • 321
  • 5
  • 18
10
votes
1 answer

Different values for nano time in different sensor data. How to figure them out?

Since the latest Android update (v. 8), I realized a very strange behavior while trying to read sensors. More specifically, I am speaking about WiFi and Cell Towers. Here are two examples: While I read WiFi access point information data and try to…
Moh
  • 1,157
  • 3
  • 10
  • 28
1
2 3
9 10