static long getTime() Gets the current value of the hires timer, in ticks.

static long getTimerResolution() Obtains the number of ticks that the hires timer does in a second.

so getTime gives you just some arbitrary number which represent some time interval you don't know about. With getTimerResolution you can calculate the time in seconds. Now if you multiply everything with 1000 you get the time in milliseconds.

to make code like this more clear to others I like to use TimeUnit so you can write it like this

milli comes from Latin and means 1000(thausend), like Millenium(1000 years) millimeter (1/1000th of a meter) and so on.so because of this; 1000 milliseconds are 1 second; 1 millisecond is 1/1000th of a second.

thx for explain I'm sorry I was really stupid ...for example when I call Sys.getTime() it return 6034252then I multi it with 1000it became 6034252000finally I divide it with Sys.getTimerResolution() which return 1000 then the result is 6034252so I don't know why we need multi 1000...

sure in this case it is not necessary, because it seems as if the number you get from Sys.getTime() is already in milliseconds, but...!!When you are offered from a lib such methods use them, because you don't have a guarantee that you will always get 1000 from Sys.getTimerResolution(), maybe you get on another PC another value. Or they change it with a newer version of the libary.

Your programm should never depend on internals of a lib, always depend on the contracts the libary is offering you.

Here the only thing say is that if you want the time in seconds divide, so divide^^

You are correct about Sys.getTime() returning milliseconds on some platforms, however since LWJGL is cross platform its not guaranteed that every platform will do the same and the value could even change on current platforms in future LWJGL releases.

Ticks can have a lower accuracy than milliseconds (can be harware related, like some old arcade machines had hardware timers that ran at 60 ticks a second as games on it only need 60fps) or might even have higher accuracy than milliseconds (like nanoseconds or more in the future), it all depends on the underlying platform.

So Sys.getTime() will return the number of ticks (whatever the implementation underneath thinks is best for platform) and then Sys.getTimerResolution() will tell you how many of those ticks there are in a second. From which you can calculate the timer resolutions you require (millisecond, microseconds, nanoseconds, picoseconds, femtoseconds, etc).

which of course is bad api design, because everything of this should get abstracted away

edit:sry, to tired ampy today I guess^^It is ok as is in someway, but in the end there is no reason why the user would need to have access to those both values. Just provide the time in seconds(floating point).

The only bad part of the API is the misleading method name, as getTime() does not return time, it returns ticks and therefore it should be named getTicks(), and getTime() should have called both existing methods, returning something the developer would expect.

Hi, appreciate more people! Σ ♥ = ¾Learn how to award medals... and work your way up the social rankings!

which of course is bad api design, because everything of this should get abstracted away

LWJGL itself is a low level API and just exposes API's otherwise not available, its really the job of the library that sits on top of it to do any abstraction. Besides why should a low level API limit you to a fixed resolution ? e.g. milliseconds or nanoseconds, especially considering that these are not available on all hardware

The above API was added in a time when System.currentTimeMillis() was the only timer available in Java and the OS's at the time couldn't even provide millisecond accuracy. Even though we have System.nanoTime() now the LWJGL timer still continues to work equally well and will do so for even higher level of timer resolutions well after both System.nanoTime() and System.currentTimeMillis() become obsolete.

If standing the test of time like that isn't a sign of solid low level API design I'm not sure what is.

The only bad part of the API is the misleading method name, as getTime() does not return time, it returns ticks and therefore it should be named getTicks(), and getTime() should have called both existing methods, returning something the developer would expect.

@Riven agreed, good point however technically it is still returning the current time, just in ticks.

thx now I had read yours replysso I had learn more...but the lwjgl 's timer tutorial still don't explain why Sys.getTime() * 1000 then / Sys.getTimerResolution() will return millisecond?then how can i calculate nanosecond?

thx now I had read yours replysso I had learn more...but the lwjgl 's timer tutorial still don't explain why Sys.getTime() * 1000 then / Sys.getTimerResolution() will return millisecond?then how can i calculate nanosecond?

Its pretty simple, there are 1000 milliseconds in a second. Sys.getTime() will return the current time in ticks. To convert from ticks to milliseconds you simply multiply by 1000 and divide by the number of ticks in a second (Sys.getTimerResolution()).

Sys.getTime() * 1000000000 / Sys.getTimerResolution(); will give you time in nanoseconds since there are 1 billion nanoseconds in a second.

Incidentally, I'd advise using a constant for that 1000000000 or using the java 7 feature allowing underscores in numbers and write it 1_000_000_000. I can't tell you how many times I've accidentally left a zero out or stuck an extra one in -- tho in the latter case for this one number you're a little safer, since it's a compiler error if you don't have an L suffix.

I'm sorry but I think you may miss my questionnow I will explain it more:(please read it Thx)1.I know getTime() will return the time in ticks,for e.g when I call getTime() 1000 times perSecond on a machine which getTimerResolution() will return 1000 , it will return a long which interval is 1;2.if now there is a machine which getTimerResolution() will return 100, so I call getTime() it's interval will be 10(= 1000/100)right?then for e.g when i call getTime() in two continuous ticks will return 6000 and 6010so now calc them to millisecond(in fact, I don't understand what millisecond here?)6000*1000/100(getTimerResolution())= 600006010*1000/100=60100now I calc the delta time :60100-60000=100(what is the unit?millisecond?why?I can't understand!!)thx for read it for so my ugly English grammer...wait for your help..

You are inventing numbers and concepts everywhere. getTime() returns the time in ticks, which is a unit that doesn't exactly mean anything until you get a resolution, which is what getTimerResolution() returns. getTimerResolution() returns how many ticks there are in 1 second. Therefore if getTime() returns 100, and getTimerResolution() returns 100, then 1 second has passed. If getTime() returns 10,000 and getTimerResolution() returns 1000, then 10 seconds have passed. And son on and so forth.

You want to convert that 1 second to millisecond, so we just multiply it by 1000.

oh I must make an large mistake....but I still can't understanding what is getTime() return you say if getTime = 100 and getTimerResolution = 100 then 1 second pass why? how you know ? I can't understand there are what connect between 100 and 100 ....end why I call getTime return values like these 21555184215552012155521721555234and my getTimerResolution() return 1000it means how many time pass?sorry for my stupid...

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org