Posted 09 April 2016 - 08:55 PM
So, I've always wondered whether or not timers could be made to go faster than the default 0.05 seconds they can go at.
After a bit of testing, I've created a program that should allow for much, much higher precision for timers.
Basically, it works by doing as many addition operations it can within a second, automatically yielding in the same loop if necessary. The amount it can do can then be used as a base for further timers.
For example, if it can do 2000 loops in one second, then 1000 should be half a second, and so on.
Except, now that we have such a large number, we can have a "preciseness" of 1 / amount.
In this case, doing 2000 loops gets you a preciseness of 0.0005, as opposed to 0.05, the default value.
Of course, since this type of timer is based solely on the speed of the CPU, there's a function in the API that will automatically adjust the base number used based on the margin of error for the time passed. Though, just know that this will only work for timers that use a number which is a multiple of 0.05.
Now, how is this useful? Well, with this, you can have an FPS of 60 or more. Well, technically.
You see, even though your program may be running at an FPS of 60, from what I know, the screen will always be capped at 20 FPS. Do prove me wrong though, if you like. I haven't done extensive testing on this.
Another thing that should be noted is that this works in the way sleep would; It waits the requested time, and doesn't queue any events. So, that kind of kills some functionality.
All in all, however, I found this to be rather interesting, and I made it simply to make a proof of concept. You can find the code here (QY5cjxXL) if you want to mess around with it.
To use it, you simply post the code to the top of your program.
After, there are two functions you have to use: HPT.startTimer(number) and HPT.adjustTimer(number)
It's highly recommend that you do something like
This line fine tunes the timer to make it more accurate.
After that, you can call HPT.startTimer() to use the API.
If you want a specific FPS, you can just do HPT.startTimer(1 / FPS).
After a bit of testing, I've created a program that should allow for much, much higher precision for timers.
Basically, it works by doing as many addition operations it can within a second, automatically yielding in the same loop if necessary. The amount it can do can then be used as a base for further timers.
For example, if it can do 2000 loops in one second, then 1000 should be half a second, and so on.
Except, now that we have such a large number, we can have a "preciseness" of 1 / amount.
In this case, doing 2000 loops gets you a preciseness of 0.0005, as opposed to 0.05, the default value.
Of course, since this type of timer is based solely on the speed of the CPU, there's a function in the API that will automatically adjust the base number used based on the margin of error for the time passed. Though, just know that this will only work for timers that use a number which is a multiple of 0.05.
Now, how is this useful? Well, with this, you can have an FPS of 60 or more. Well, technically.
You see, even though your program may be running at an FPS of 60, from what I know, the screen will always be capped at 20 FPS. Do prove me wrong though, if you like. I haven't done extensive testing on this.
Another thing that should be noted is that this works in the way sleep would; It waits the requested time, and doesn't queue any events. So, that kind of kills some functionality.
All in all, however, I found this to be rather interesting, and I made it simply to make a proof of concept. You can find the code here (QY5cjxXL) if you want to mess around with it.
To use it, you simply post the code to the top of your program.
After, there are two functions you have to use: HPT.startTimer(number) and HPT.adjustTimer(number)
It's highly recommend that you do something like
HPT.adjustTimer(3)
In the beginning of your code before you start to use the timer.This line fine tunes the timer to make it more accurate.
After that, you can call HPT.startTimer() to use the API.
If you want a specific FPS, you can just do HPT.startTimer(1 / FPS).
Edited on 12 April 2016 - 12:59 PM