Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  One qestion about lwjgl's getTime()  (Read 4494 times)
0 Members and 1 Guest are viewing this topic.
Offline wdl7770016

Senior Newbie





« Posted 2012-08-04 05:16:47 »

now there is a method in lwjgl's base tutorial  Timing
1  
2  
3  
public long getTime() {     
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
 }


what I want to know is that why Sys.getTime() need multi with 1000 and then div with Sys.getTimerResolution()?what does it mean?
please explain it to me step by step!! thx!!
Offline Danny02
« Reply #1 - Posted 2012-08-04 06:35:24 »

from the api:

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
1  
2  
3  
4  
public static long getTime()
{
        return SECONDS.toMillis(Sys.getTime() / Sys.getTimerResolution());
}

Offline wdl7770016

Senior Newbie





« Reply #2 - Posted 2012-08-04 09:15:56 »

uh  thx for help me
but I still can't understand why any number multi with 1000 it will be millisecond?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2012-08-04 09:59:51 »

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.

To help you further with this basic, basic math:
1s = 1000ms

so i.e.
45,3s = 45,3(1000ms) = 45300ms
342ms = 342(1/1000s) = 0,342s

Offline wdl7770016

Senior Newbie





« Reply #4 - Posted 2012-08-04 10:35:47 »

thx for explain
I'm sorry I was really stupid ...
for example  when I call Sys.getTime() it return 6034252
then I multi it with 1000
it became 6034252000
finally I divide it with Sys.getTimerResolution() which return 1000
then the result is 6034252
so I don't know why we need multi 1000...
Offline Danny02
« Reply #5 - Posted 2012-08-04 11:00:50 »

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^^
Online kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #6 - Posted 2012-08-04 12:07:24 »

The tutorial here has an explanation about it.

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).
Offline Danny02
« Reply #7 - Posted 2012-08-04 13:41:27 »

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).
Online Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-08-04 13:54:30 »

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
Online kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #9 - Posted 2012-08-04 18:11:16 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wdl7770016

Senior Newbie





« Reply #10 - Posted 2012-08-05 15:41:14 »

thx now I had read yours replys
so 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?
Online kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #11 - Posted 2012-08-05 16:02:28 »

thx now I had read yours replys
so 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.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #12 - Posted 2012-08-05 16:58:57 »

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.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-08-06 00:11:14 »

How about the nice and simple 1e9? Smiley

Offline sproingie

JGO Kernel


Medals: 202



« Reply #14 - Posted 2012-08-06 00:13:27 »

Oh yer one of dem fancy-schmancy egghead types wid all yer sciencestistic notating!   Cranky
Online Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2012-08-06 00:14:34 »

How about the nice and simple 1e9? Smiley
1e9 is a double

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

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2012-08-06 00:16:11 »

Hey, it's better than missing a zero....unless you accidentally type 8 instead of 9 XD

Ok then: (int)1e9 or (long)1e9 Wink

Online Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2012-08-06 00:16:42 »

and how exactly is that an improvement?

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

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2012-08-06 00:17:14 »

It was a response to sproingie's post about forgetting a zero.

Also @sproingie, an int can store 1000000000, it's not a compiler error if there isn't an L.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 784
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #19 - Posted 2012-08-06 00:18:30 »

You traded one error prone approach with another.


Pre-java-7 I did: 1000L*1000L*1000L, now I do 1_000_0000_000L

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

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #20 - Posted 2012-08-06 00:20:01 »

What are you trying to prove? That was just a quick reply to sproingie. Nothing's not error prone anyway, there's always an idiot somewhere who will mess up an idiot-proof system.

Also, to prove my first point, you added an extra zero in there Wink

Offline sproingie

JGO Kernel


Medals: 202



« Reply #21 - Posted 2012-08-06 00:23:41 »

An int is 32 bits and cannot store 10 billion.  Try it.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #22 - Posted 2012-08-06 00:34:03 »

Right but we're talking about 1 billion :S

Offline wdl7770016

Senior Newbie





« Reply #23 - Posted 2012-08-06 04:24:00 »

I'm sorry but I think you may miss my question
now 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 6010
so now calc them to millisecond(in fact, I don't understand what millisecond here?)
6000*1000/100(getTimerResolution())= 60000
6010*1000/100=60100
now 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..
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #24 - Posted 2012-08-06 06:19:48 »

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.

Any better?

Offline wdl7770016

Senior Newbie





« Reply #25 - Posted 2012-08-06 08:43:20 »

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
21555184
21555201
21555217
21555234
and my getTimerResolution() return 1000
it means how many time pass?
sorry for my stupid...
Offline Danny02
« Reply #26 - Posted 2012-08-06 09:53:17 »

what if you just let this pass and simply use the method which returns milliseconds.
Perhaps when you look back at this in a few weeks you will have your eureka moment.
Offline wdl7770016

Senior Newbie





« Reply #27 - Posted 2012-08-06 12:16:19 »

OK  I will just use it now
it's principle make me really a headache...
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #28 - Posted 2012-08-06 18:56:03 »

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

getTime() returns the time in ticks
getTimerResolution() returns how many ticks there are in 1 second


Offline wdl7770016

Senior Newbie





« Reply #29 - Posted 2012-08-07 03:52:05 »

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

getTime() returns the time in ticks
getTimerResolution() returns how many ticks there are in 1 second



Sor I mean that what is time in Ticks? What is Ticks?...
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!