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.

146 questions
160
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
  • 43,605
  • 46
  • 133
  • 171
39
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
  • 391
  • 1
  • 3
  • 3
36
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
  • 22,412
  • 8
  • 58
  • 84
36
votes
7 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
  • 10,085
  • 22
  • 65
  • 92
32
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
  • 11,157
  • 26
  • 81
  • 110
32
votes
3 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
  • 16,775
  • 22
  • 90
  • 113
26
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,804
  • 4
  • 28
  • 32
21
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
  • 8,518
  • 14
  • 77
  • 116
16
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
  • 259,732
  • 324
  • 769
  • 1,199
16
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
  • 10,475
  • 10
  • 60
  • 98
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
  • 193,756
  • 157
  • 432
  • 708
15
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.
  • 3,427
  • 9
  • 30
  • 66
15
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,987
  • 11
  • 54
  • 85
12
votes
3 answers

How can I convert the result of System.nanoTime to a date in Java?

I want to convert the result of System.nanoTime() to a date. public void tempBan(Player p, Player banner, int timeInSeconds){ Long timeInNano = (long) (timeInSeconds * 10^9); int newTime = (int) (System.nanoTime() + timeInNano); // here…
user5305868
11
votes
4 answers

java.sql.Timestamp way of storing NanoSeconds

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; …
Vicky
  • 121
  • 1
  • 1
  • 6
1
2 3
9 10