(Interesting!) Frame Limiting done better!

Blitz3D Forums/Blitz3D Programming/(Interesting!) Frame Limiting done better!

Kryzon(Posted 2009) [#1]
Just Copy - Paste - and - run this thing:

While Not KeyHit(1)
d = d + 1
If d > 359 Then d = 0
For I = 0 to 100
t = Cos(d)*Sin(d)*Tan(d)
Next
If KeyDown(57) Then api_Sleep(10)
Wend


You need this DECLS for this to work:
http://www.freewebs.com/rafael_navega/CPU_Sleep.decls

Make sure to watch the CPU usage in your Task Manager with and without the space key being pressed.

In case you are wondering WTH does this change in my life...
We can now effectively replace parts of frame limiting codes like...
Repeat
   elapsed=MilliSecs()-FrameTime
Until elapsed

...with the apropriate time in Api_Sleep(milliseconds).

Credits go for Boiled Sweets for making the Kernel32.DECLS


Gabriel(Posted 2009) [#2]
Would you mind explaining what you think this code does?


Kryzon(Posted 2009) [#3]
Would you mind specifying which code you are refering to?


Gabriel(Posted 2009) [#4]
The code I'm supposed to copy and paste and run. The code which is supposed to be a better way to frame limit.


GIB3D(Posted 2009) [#5]
I tried it, and it made my cpu for the program go from 50 to 0 while pressing the button :D


Gabriel(Posted 2009) [#6]
I tried it, and it made my cpu for the program go from 50 to 0 while pressing the button :D

Yes, that's what it did for me too. Which is why I was wondering what Kryzon thinks it does and what application he thinks it has for frame limiting, as well as what makes it superior to some other method.


Kryzon(Posted 2009) [#7]
That is only to show the application of the Api_Sleep call.
I know the topic title is a little misleading (I wasn't sure what to put when I created it, and there isn't a topic title editing feature in these forums).

I remember reading in frame limiting discussions that there aren't really ideal manual solutions for frame limiting because most of them rely on putting the CPU through a useless blank loop until the frame time is met (I think it was with Ross C, don't recall ), and then iterating through the rest of the program (even if it's only for a single millisecond, it's still, logically speaking, unoptimized).
Now there is something better to put in the "waiting" part. Api_Sleep.

The snippet is merely for showing that the function does not fails, it really does free the CPU.


Stevie G(Posted 2009) [#8]
Isn't "api_Sleep( 10 )" exactly the same as "delay 10" which also free's the CPU?


Kryzon(Posted 2009) [#9]
What evidence do you have that Delay free's the CPU?


Gabriel(Posted 2009) [#10]
That is only to show the application of the Api_Sleep call.

Yes, I realize that. I just haven't managed to grasp what you're saying the application of it is. I know that it frees CPU time, but all my tests suggest that utilizing the CPU yourself is more effective at creating good game timing than giving it back to the OS is, so it seems at cross purposes to my experience. Which is why I was hoping for a little more information on what you think or have experienced as a benefit from giving the OS back some time.

What evidence do you have that Delay free's the CPU?

Well, replace Api_Sleep(10) in your example with Delay(10) and you'll find it has exactly the same effect. AFAIK, StevieG is correct. Delay is calling Sleep internally.


Stevie G(Posted 2009) [#11]

What evidence do you have that Delay free's the CPU?



It's a well known fact. I always use a small delay in my main loop just incase another task needs some cpu.


Sledge(Posted 2009) [#12]
And you can always check CPU usage with Task Manager.


Kryzon(Posted 2009) [#13]
Oh-kay then!
Sleep = Delay. It does have a really similar (if not equal) result by replacing Api_Sleep(10) with Delay(10) ).

Good day al'around.


It's a well known fact.


That doesn't prove anything (it was a well known fact that the earth was flat! and look how that ended up), but I see your point totally.


H&K(Posted 2009) [#14]
it was a well known fact that the earth was flat!
No it wasnt

Anyone in history or pre history of any note, who expressed an opinion thought that the world was round, or roundish (pear shapped etc)
The assumption that people thought the world was flat only really started in about 1830 when Washington Irving "made up" his history of columbus' vouage to the Americas, and Antoine-Jean Letronne's On the "Cosmographical Ideas of the Church Fathers"


jfk EO-11110(Posted 2009) [#15]
Not only Delay lets your CPU "breath", but as far as I recall also VWait and Flip True.


Kryzon(Posted 2009) [#16]

The assumption that people thought the world was flat only really started in about 1830


"Various cultures have had conceptions of a flat Earth, including ancient Babylon, Egypt, pre-Classical Greece and pre-17th century China." - taken from the same article you appear to have referenced.


Not only Delay lets your CPU "breath", but as far as I recall also VWait and Flip True.


But did Mark say anything to back that up, or have you taken a look at the B3D SDK source? (instead of just personal tests - which are still reliable of course, but I'd prefer something more concrete)


jfk EO-11110(Posted 2009) [#17]
You only have to watch the running tasks with something like process explorer by sysinternals.


Adam Novagen(Posted 2009) [#18]
You only have to watch the running tasks with something like process explorer by sysinternals.

Or you could just hit Ctrl+Alt+Del and check the Task Manager. ^_


BIG BUG(Posted 2009) [#19]
Not only Delay lets your CPU "breath", but as far as I recall also VWait and Flip True

Nope, your totally wrong.
Flip True actually stresses the CPU pretty hard as it behaves like an empty loop. I guess VWait works the same way.


Adam Novagen(Posted 2009) [#20]
Question: unless you're making something like a 3D modeler, then you're probably making a game, which assumes that the player is playing it, so... Why would you NEED to free CPU time for other applications??


LineOf7s(Posted 2009) [#21]
Because some (many?) games - especially so-called "casual" games - are played in a window whilst other applications are running. Like when someone has a quick game of Solitaire (for example) while waiting for something else to happen...

Plus, people like to multitask these days with their new-fangled computers that are able to do so, so it's just polite to allow for that possibility, rather than having your dinky lil proggie consume an entire CPU if it doesn't need to.

...then again, some games need as much as they can get...

Horses for courses.


BIG BUG(Posted 2009) [#22]
Also, a CPU running at full capacity wastes a lot of energy and causes a more noisy computer, as the fan needs to run at full RPM too...

Btw. WaitTimer does free the CPU too...


jfk EO-11110(Posted 2009) [#23]
Ok, you're right, I was wrong with VWait and Flip true. I hope this isn't this "Alz-Thingie", you know what I mean. I'm still pretty sure at least once upon a time Flip 1 and Vwait didn't eat 95% of the CPU cylces, as they do now.


Kryzon(Posted 2009) [#24]

Btw. WaitTimer does free the CPU too...


Boy, if that's true, than it's a far better command than I'd ever expect (from reading bad things about it here, don't remember where).


epiblitikos(Posted 2009) [#25]
I had also heard some bad stuff about WaitTimer, I definitely want to give the timer system another look now. Thanks BIG BUG.

PS - VSyncing uses polling in Blitz and so is definitely, definitely CPU intense.

[EDIT:]

Looked into the manual and sure enough on the CreateTimer page it mentions the CPU-freeing. I really gotta read that stuff more. Again, thanks BIG BUG.