Android Question Trying to call System.nanoTime()

Roger Taylor

Member
Licensed User
Longtime User
I've been looking at JavaObject and Reflection but this is as far as I've gotten with the getting the value from System.nanoTime. I'm not sure if System.nanoTime is a method or a read field or how to add the System library to my project.

What I'm ultimately trying to do is retrieve a running time value (date isn't important). Hopefully the performance tax isn't high on getting this value but I do need to nanosecond or microsecond precision/accuracy. I don't need a millisecond value that's simply mathematically converted to nanoseconds or microseconds.
 

drgottjr

Expert
Licensed User
Longtime User
a way:
B4X:
    Dim jo As JavaObject
    jo.InitializeStatic("java.lang.System")
    Dim nanotime As Long = jo.RunMethod("nanoTime", Null)
    Log(nanotime)                        ' nanoseconds

i'm guessing there is a b4x or b4xpages way
 
Upvote 0

Roger Taylor

Member
Licensed User
Longtime User
@stevel05 has published a nanotime library in the forum. "nanotime" as keyword can't search it out in the forum?

The bigger question is why are libraries being maintained as forum posts? This is the most unusual thing I've seen. Besides, sometimes just asking in the forums makes for good chit chat and we can learn even more. Admittingly, I usual google my way into threads of interest and this is how I sometimes find libraries besides using the search field here.
 
Upvote 0

Roger Taylor

Member
Licensed User
Longtime User
a way:
B4X:
    Dim jo As JavaObject
    jo.InitializeStatic("java.lang.System")
    Dim nanotime As Long = jo.RunMethod("nanoTime", Null)
    Log(nanotime)                        ' nanoseconds

i'm guessing there is a b4x or b4xpages way
Thank you for the code example! I'll see what I can do with this, and maybe learn more about how to use JavaObject to pull off some tricks.
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
Hi Roger, I am thinking maybe '"nanotime" as keyword can't search it out in the forum?' causes some misunderstanding, it is not a blame at all as I used steve's nanotime lib before so I know it is somewhere but curious maybe the post subject is not nanotime.

I see now there is a library update post in the forum and also a managed excel file for a whole picture, but usually I just searched the forum by keyword and it works great.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
nanosecond or microsecond precision/accuracy
I doubt you will get either - spurious precision perhaps but even millisecond accuracy highly unlikely as there are many threads running in parallel with yours in Android sharing one or more of the CPU cores. Android is not a realtime OS.
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
The bigger question is why are libraries being maintained as forum posts?
Click the 'Online Table' link
 
Upvote 0

Roger Taylor

Member
Licensed User
Longtime User
I doubt you will get either - spurious precision perhaps but even millisecond accuracy highly unlikely as there are many threads running in parallel with yours in Android sharing one or more of the CPU cores. Android is not a realtime OS.

I'm not trying to do a specific task every nanosecond. This would be ridiculous at software level.

The fact that a nanosecond timer even exists suggests that it's done at hardware level. If the hardware maintains the clock then this should be good enough for my application on any modern device it's run on.

At any rate my app can acquire the readings, it should be possible to deduce where a raster beam should be on an emulated CRT, for example.
 
Upvote 0
Top