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
5
votes
3 answers

convert python time.time() to java.nanoTime()

java's System.nanoTime() seems to give a long: 1337203874231141000L while python time.time() will give something like 1337203880.462787 how can i convert time.time()'s value to something match up to System.nanoTime()?
4
votes
2 answers

ScalaTest: treat Instants as equal when in the same millisecond

I have a case class that I serialize to JSON, and a test case that checks that round-tripping works. Buried deep inside the case class are java.time.Instants, which I put into JSON as their epoch milliseconds. Turns out that an Instant actually has…
Thilo
  • 257,207
  • 101
  • 511
  • 656
4
votes
1 answer

System.nanoTime() gives wrong times in Android

I am developing a game where some events happen after some predetermined amounts of time. For that I need to calculate how much time has elapsed from the starting of the game. I do that by launching a thread in the main loop and computing the time…
CHebdo
  • 41
  • 3
4
votes
1 answer

Android: time intervals with deep sleep (System.nanoTime(), System.currentTimeMillis(), SystemClock.elapsedRealtimeNanos())

I am implementing an application that has a minimum API level 14 (this is important) and requires consistent interval measuring. No need for ms precision, it just needs to be always counting the time (elapsed seconds). So far, to treat time…
walljam7
  • 343
  • 2
  • 11
4
votes
1 answer

Java - Thread.sleep inside run method

I'm following a tutorial and below is the run method to generate logic and frame updates. I understand how the ticks are updated 60 ticks/second but I don't understand how we adjust frame per second here. Right now with Thread.sleep(2), frame per…
user3421485
  • 45
  • 1
  • 1
  • 5
3
votes
1 answer

Intercepting and changing the return value of a static native method with byte-buddy (System.nanoTime())

I want to change what happens when System.nanoTime() is called for arbitrary Java programs (I want to shift back time to help container checkpoint/restore use cases). Whenever System.nanoTime() is called, I want to run the original System.nanoTime()…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
3
votes
1 answer

Safe time origin for System.nanotime()

I am using a variable time_of_last_call as my origin of time; since nanoTime() might give negative values, I can’t use 0 as my origin of time to initialize time_of_last_call. If I initialize time_of_last_call with Long.MIN_VALUE I can have overflow…
user20191130
  • 340
  • 1
  • 9
3
votes
1 answer

Java - ArrayList Objects not deleted properly

I am working on a 2D platformer game for my, last, HS year project. The game is basically about a player walking back & forward, collecting points and reaching goals... The player can shoot bullets and when bullets hit a block, it is destroyed. Now,…
Feelsbadman
  • 1,163
  • 4
  • 17
  • 37
3
votes
0 answers

System.nanoTime() inaccurate on Android?

I've discovered through extensive printf-debugging that System.nanoTime() isn't actually what it purports to be: a monotonically increasing long representing nanoseconds with at least millisecond accuracy. I've made two calls to System.nanoTime()…
MitchellSalad
  • 4,171
  • 8
  • 23
  • 24
3
votes
3 answers

Accuracy of System.nanoTime() to measure time elapsed decreases after a call to Thread.sleep()

I'm encountering a really unusual issue here. It seems that the calling of Thread.sleep(n), where n > 0 would cause the following System.nanoTime() calls to be less predictable. The code below demonstrates the issue. Running it on my computer (rMBP…
3
votes
1 answer

How to handle when System.nanoTime() returns the same value between calls?

This question is not intended as an attack upon System.nanoTime(). I realize it is a surprisingly tricky method to use correctly. What are some ways to deal with System.nanoTime() returning the same value between calls? Example: Multiple threads…
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
3
votes
3 answers

(Delta time) Getting 60 updates a second in java

I've seen this code several times. long lastTime = System.nanoTime(); final double ticks = 60D; double ns = 1000000000 / ticks; double delta = 0; The code above takes the System time and stores it to lastTime. The 60 ticks should equate to the…
Shadow
  • 351
  • 2
  • 5
  • 15
3
votes
2 answers

How can the "<" or ">" operators overflow?

I am intrigued with the following statement found in the documentation of the System.nanoTime() method in Java: long t0 = System.nanoTime(); ... long t1 = System.nanoTime(); One should use t1 - t0 < 0, not t1 < t0, because of the possibility of…
2
votes
3 answers

Synchronize Java Virtual Machine with System.nanoTime

Does it make sense to synchronize java virtual machines with System.nanoTime () ? I mean : A call System.nanoTime () and puts the results in t1 A send a packet to B when the packet from A is received on B, B call System.nanoTime() and sends the…
jean
  • 251
  • 4
  • 14
2
votes
3 answers

Why does chrono::system_clock returns microseconds whereas clock_gettime returns nanoseconds

std::chrono::system_clock::time_since_epoch().count() gives me a result in microseconds. I want the current time in nanoseconds. But I can't use high_resolution_clock because on my system it is an alias on steady_clock (the monotonic clock). I know…
rtur
  • 328
  • 2
  • 16
1 2
3
9 10