Controlling loop cycle speed.
Author |
Message |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: Thu Jan 06, 2011 6:01 am Post subject: Controlling loop cycle speed. |
|
|
So in my tests my turing runs like, over 900,000 times in 1 minute. I want this to run only a measly 3600 times instead...
Now i don't want to reply on "delays" to really do this, since that really effects the program differently to each persons computer.
Optimally, I want a program to count to 60 within 1 second, for 60 seconds, for 3600 loops in a min.
I'm not sure if i should try to set this up using Time.Elapse as the longer the program goes, well yeah, you know just doesn't seem all that logical to use.
Any ideas? shoot em' out!...
///My idea was to have it check Time.Elapse, anyone else can think of a better way? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Thu Jan 06, 2011 7:35 am Post subject: RE:Controlling loop cycle speed. |
|
|
This is two ways of controlling frames per second (FPS) that I know of. You can also look up the Time.DelaySinceLast(1000 / 60). The reason why I didn't use it for this example is because it hides information (but makes it simplier).
Basically, this will slow down the program using a calculated delay when running too fast. The other is to move objects according to time for when it's running too slow. The problem with the latter is that you can sometimes bypass collisions if a computer was lagging really bad. So that method isn't exactly advised unless you deal with everything accordingly.
Turing: |
% Since counting the framerate is considered logic, and blitting/rendering
% the scene also takes (rather alot) of time, standard practice would be to have the
% framerate calculation (Time.Elapsed - lastT) to include the render section.
% You would then have the logic of the current frame react to the framerate of the
% last frame.
View.Set ("graphics,offscreenonly,nobuttonbar")
var lastT, codeTook, frame, codeFPS, controledFPS, howMuchDelay, renderingTime : int := 0
var targetFPS := 60
var n : int := 50000 % 1000 to test fast, 1000000 to test slow
var angle : real
loop
% --- Logic ---
lastT := Time.Elapsed
% Count to n
for i : 1 .. n
end for
frame + = 1
codeTook := Time.Elapsed - lastT
if codeTook = 0 then
codeFPS := maxint
else
codeFPS := 1000 div codeTook
end if
howMuchDelay := (1000 div targetFPS ) - codeTook
if howMuchDelay > 0 then
% Code is running too fast!
% Using a delay based on how fast your code ran will
% make sure each frame will be as close to the same
% time taken as possible.
% Try: n = 1000
% 1 second divided by how many frames your target is,
% then substract the time already spent processing code
Time.Delay (howMuchDelay )
end if
codeTook := Time.Elapsed - lastT
controledFPS := 1000 div codeTook
% Having movement based on time will let any slow machines
% play in real time but suffer major frame skips.
% Try: n = 1000000
renderingTime := Time.Elapsed
angle := renderingTime / 10
% --- Render ---
cls
Draw.FillOval (round (maxx div 2 + cosd (angle ) * 80), round (maxy div 2 + sind (angle ) * 80), 10, 10, red)
put "Frame: ", frame
put " Counting to: ", n
put " Code Took: ", codeTook, " ms"
put " Code FPS: ", codeFPS
put " Delay: ", howMuchDelay
put " Controled FPS: ", controledFPS, " (Target = ", targetFPS, ")"
View.Update
% Adjust number to count to based on how fast the code took to run
% Not really neccessary for FPS control but makes the demo more
% dynamic and finds the target for the current system (benchmark?)
if codeFPS > controledFPS then
n + = 1000
elsif codeFPS = 0 then
else
n - = 1000
end if
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
|
|