Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / programming and compiling questions from a newbie

Author
Message
Roses In Dreams
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: Somewhere outthere.
Posted: 6th Feb 2008 09:33
Im new to programming and DBpro.I started this year and I have some questions.

while-endwhile


repeat-until


for-next


do-loop


All of these stuff, do the same thing. I noticed that all of those boil down to a do-loop statement.
Can someone tell me what are the differences of those looping commands?
Im really concerned with speeding up the program. I also want to know if I had 1000 of these loops,which of the commands would do the exact process the fastest?
Roses In Dreams
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: Somewhere outthere.
Posted: 6th Feb 2008 09:35
my program crashes when i clone sprite 1000 times but does fine around 500 times.

What exactly was happening?
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 6th Feb 2008 11:05 Edited at: 6th Feb 2008 11:06
First example:
No sign of setting B's values, so is 1 less than 0?
No it's not, so the statements between while and endwhile will never be run because the condition for running the while loop was not met...

Second example:
Again, no sign of setting B's value, A starts as 1, and is increased to 2. Now your program checks if 2 is equal to 0. It's not so A is increased by one, and your program check if 3 is equal to 0. It's not, so A is increased again. You may notice that A is actually getting further away from zero.
So for now, I'll tell you that A will never equal zero, and the loop will run forever. (Actually, it will reach zero, but it might confuse you if I told you why, so, "it won't reach zero", that's the official line)

Third Example:
This starts A off at the value of 1, if it is less than the "to" value (B) it will run, and increase A's value when it hits the next statement.
So, inc A is not needed in this loop, at all, and in real projects it may cause your loop to exit early.
Also, Since B's value is currently 0 the loop will not run anyway.

Fourth Example: Well, same problem as the second really, A starts above B and is increased even further away.



Basically, the main differences between the loops are:
while-endwhile and for-next:
Check the conditions of running the loop before the loops are entered, and it's possible they're never run.

repeat-until:
Will check the conditions for running the loop after it has run through (it's checking if the loop should be run again). If you know a loop needs to run at least once, these should be marginally faster as they're not checking a logical condition before the first run.

do-loop:
Enters the loop without checking if it should be run. It is assumed that these should always run except in special circumstances such as the user pressing the escapekey. Of all of the loops, it's least likely to exit unexpectedly



As for your issue with sprites, I don't use them, so I have no idea...
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 6th Feb 2008 17:31
Quote: "Can someone tell me what are the differences of those looping commands?"


http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

Tutorial 2.

Quote: "which of the commands would do the exact process the fastest?"


The difference is negligible. What matters is how you do what is inside the loops!

TDK_Man

SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 7th Feb 2008 12:27 Edited at: 7th Feb 2008 12:33
I too thought the difference was negligible, but never bothered to test, so I would always use



I've never tried, and can't really try it out now, but would you say that:



would be slightly faster still? I don't have DB in front of me right now
Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 7th Feb 2008 17:46
I agree with TDK:



The difference is negligible.

I think that I have placed an equal load on each of the loops. I also tossed in the dreaded goto, I don't use it, but I don't play favorites when it comes to testing.


Whatever...
Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 7th Feb 2008 18:26 Edited at: 7th Feb 2008 18:53
@WindowsKiller

Yes, but. How much time do you spend in the real world codeing loops that just increase a counter?

Edit: Here is the no load version



Does it match your results?


An afterthought:
It shouldn't because your for-next test loop has a superflous (inc a) command.


Whatever...
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 7th Feb 2008 22:14
I still maintain that the difference in speed between all the loop speeds is 'negligible'. By that I mean that there is a difference, but hardly worth worrying about.

Quote: "What matters is how you do what is inside the loops!"


WindowsKiller's loops only increment an integer and one is around 60 or so ms quicker. But how many such loops appear in real life programs? None!

Instead, loops will be calculating distances, moving objects, updating the screen and all sorts of stuff. As such, there's a good chance that the speed gain using Repeat..Until over While..Endwhile can easily be counteracted by ill-optimised code inside the loop.

In one of my programs I have a fairly small 22 line particle function which uses For..Next. I timed it and at first it reported 0ms as it wasn't a very big loop and wasn't doing a lot of work.

So, I added a few float calculations until it took more than 300 ms to execute. I then changed the For..Next to a Repeat..Until and it was actually faster:

For..Next Loop: 328ms
Repeat..Until: 312ms


OK, even with a small 'working' loop, I gained just 16ms. I reckon with even busier loops, the gain would be even less - depending as I said earlier on exactly what is being done inside the loop.

That's the reason why I maintain that what is inside the loop is more important to the performance of a program than the actual method used for looping.

I am definitely not saying that there is no difference, only that it doesn't really matter that much. There are more important things to concentrate on when trying to make your programs run faster...

TDK_Man

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 7th Feb 2008 23:41
I also 'ignored' While..Endwhile. I just assumed that all loops will have differences in performance depending on what's inside the loop.

The three conditional loops you quoted times for are the ones used in most people's programs within functions and procedures.

It's only the main program loop which tends to be an unconditional Do..Loop, although many use a Repeat..Until loop for this purpose - which I personally prefer. Using Do..Loop as the main program loop in an insignificant program isn't a big deal.

On my machine, your Do..Loop benchmark took 2057ms which is considerably slower, but as an unconditional loop to time it you have to add an If..Then statement inside the loop to provide a condition.

Adding the same thing to the other loops to make an equal comparison make them twice as long to complete on my system.

TDK_Man

Roses In Dreams
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: Somewhere outthere.
Posted: 8th Feb 2008 06:35
@SimSmall
Thanks for advicing on what to avoid and when to use the loops correctly.

@Windows Killer
Thanks for testing. I'll certainly have repeat-until on the top speed priority list followed by for-next and/or while-endwhile. I'll just adjust my style to avoid code sore-eyes.

@Dabbler
Sorry about the superflous for-next. I didn't notice that. Ill remember avoiding that from this day on forward until I can no longer move my hands and finger on the keyboard.

@TDK
The slowness of do-loop is because of the if-then statement. It had to check the if-then conditional everytime.right?
Does it mean that if i had like 10000 if-then checks in repeat-until, while-endwhile, for-next and 10001 if-thens in do-loop, the SPEED DIfference in tems of percentage becomes relatively small?

To all, thanks. Ill be using the codes you gave to do speed-tests.
Roses In Dreams
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: Somewhere outthere.
Posted: 8th Feb 2008 07:00
@TDK
Quote: "Adding the same thing to the other loops to make an equal comparison make them twice as long to complete on my system."

Okay.

To all interested in helping:

Please check if my understandings are correct:
This is on user-defined functions, gosubs , select-case and main do-loops:

1. Use-defined functions are written at the endpart of the code.

2. Gosub jumplabels are written in the middle of the entire code. Possibly inside the main do-loop

3. The main do-loop is written at the beggining right after initialization.

4. the select-case is written in the go-subs or main do-loops


Other uncertain understandings:

1.No looping code including a main do-loop should exist in user-defined functions.

2.No gosubs or selectcase should exist in user-defined functions.

3.No gosubs should exist within a gosub jumplabel.

4.No select-case should exist within a select-case.

5.No user-defined functions should be written within a user-defined function.

Please help me clear my uncertainties.
Roses In Dreams
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: Somewhere outthere.
Posted: 8th Feb 2008 07:09 Edited at: 8th Feb 2008 07:11
Sorry about having to ask this basic questions. I was just worried about doing my own testing and getting crashes all the time.

By the way, Is it really true that if a program crashes that there is a small chance that some of your softwares or hardwares get affected?

Please also check my request on the previous message(at the top of this message)
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 8th Feb 2008 09:59 Edited at: 8th Feb 2008 09:59
Quote: "1. Use-defined functions are written at the endpart of the code."


Official line: "Yes" again, it's not strictly true, ut code is much easier to read, and more efficient if you do this

Quote: "2. Gosub jumplabels are written in the middle of the entire code. Possibly inside the main do-loop"


If you're meaning.
gosub myJumpLabel inside your loop, and
myJumpLabel: outside your loop, then you're spot on.

Your main game loop should be as few lines as possible, preferably consisting only of gosubs or function calls. This way you only read bits of code relevant to that subroutine (such as handling the camera) and not code for other subroutines (such as moving the player). When all of the relevant code is grouped together, it's easier to debug if something goes wrong.

Quote: "3. The main do-loop is written at the beggining right after initialization."


Not necessarily, suppose you want a nice graphical menu before loading your main game, this would likely be it's own loop that goes ahead of your main game loop

Quote: "4. the select-case is written in the go-subs or main do-loops"
definately not in the main loop. Write it with the function sub that uses it. If none of your existing functions or subs use it. Determine whether it should even be in your code, or if you should delete it.



Quote: "1.No looping code including a main do-loop should exist in user-defined functions."


Why not... to determine an object number that has not yet been used, so you can assign it to an object you are about to create


Quote: "3.No gosubs should exist within a gosub jumplabel."


There's nothing wrong with further dividing existing subroutines. An example being moving the player.
One branch for getting the list of currently being pressed,
One for translating those keys into which action they perform,
And one more for performing the actions.

Quote: "4.No select-case should exist within a select-case."


Why would you need to do this?

Quote: "5.No user-defined functions should be written within a user-defined function."


Ah, the biggie... You cannot declare a function within another function. You can leave it as that, or I can attempt to explain why (in the code box)

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 8th Feb 2008 14:56
Quote: "Quote: "3. The main do-loop is written at the beggining right after initialization."

Not necessarily, suppose you want a nice graphical menu before loading your main game, this would likely be it's own loop that goes ahead of your main game loop"


This I don't agree with as it becomes difficult to maintain procedurally.

By all means have the code in another loop, but place that loop in a procedure and Gosub it at the start of the program - after the initialisation and before dropping into the main loop.

When the main menu procedure is exited, program control drops back to the line after the calling Gosub and drops into the main loop - starting the game.

This way, you don't have the problem of jumping to the menu code at the start of the program when the game ends - you just use Gosub again.

It's covered in tutorial 2 here:

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

TDK_Man

Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 8th Feb 2008 16:33
Quote: "4. the select-case is written in the go-subs or main do-loops"

It depends what you're using it for. If you're using it to control the flow of the main loop then it's fine there. Otherwise, it can be beneficial to move it to a function.

Quote: "4.No select-case should exist within a select-case."

It can, and sometimes it's necessary.

Quote: "function findFirstObject()
for x = 1 to 65535
if object exist(x) = 1 then exit
next x
endfunction x"

I really wouldn't suggest using this for everything, as the more objects you use the longer it'll take each time to find a free object number. That and you're actually returning from the function when it finds an object that exists.

Roses In Dreams
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: Somewhere outthere.
Posted: 9th Feb 2008 14:30
@Benjamin
Quote: ""4.No select-case should exist within a select-case."
It can, and sometimes it's necessary."

I never thought it was possible. I'll go look for some codes with this case.

Quote: ""4. the select-case is written in the go-subs or main do-loops"
It depends what you're using it for. If you're using it to control the flow of the main loop then it's fine there. Otherwise, it can be beneficial to move it to a function.
"

A select case in a function. I guess you can use this for making a function returning a value undergoing different processes basing on cases or maybe do other commands basing on case while avoiding the select-case from being placed in the gosub jumplabels or main loops.right? Initially, i thought select cases are not good placed in functions because i never saw anything done like it.

@SimSmall
Ill try to absorb the things you just said.
Quote: " You cannot declare a function within another function. (in the code box)"

Okay.There are global functions and local functions. I only thought there were local and global variables. (i read TDK's tutorial and the helpfiles) Or maybe, it was just me not noticing it.


Im gonna readjust my understanding and my coding structuring.

Ill sure be on the look-out for those uncommon instances you have just mentioned. Thanks for informing me that because I might never realize it that easily. I might even not figure that out in years.
Roses In Dreams
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: Somewhere outthere.
Posted: 9th Feb 2008 14:55
I still have some basic questions.

These ones are on external files, arrays, queue and memblock.
(I read TDK's tutorial 4 and the DBpro helpfiles..Im still trying to absorb the informations correctly)

Tell me If Im saying something wrong or missing something:

An external file can be a text file in different format, an image with RGB data and ASCII data. These can be loaded into an array using convertions like the str$(), val(), ascii() get rgb value() and some other stuff. By loading the external data into a multi-dimensional array, correctly arranged data can be easily be accessible.

Why should i need queue if I can just use any array directly?

I read something on DWORD and MEMBLOCKS on some threads. SOrry for being so doubtful but It's more comfortable to read a credible tutorial or thread like TDKs.

DWORDs are variables that can be any 256 characters in every byte. A memblock is an arrangement of data in cubes. It makes programs run faster. Memblocks are efficient for converting to mesh or image.

Thats all that I understand. If i should be reading a thread, please direct me.

Thanks for all the help guys.
SimSmall
20
Years of Service
User Offline
Joined: 7th Aug 2004
Location: United Kingdom
Posted: 9th Feb 2008 15:11
Not exactly what I meant. there are no local functions, ever... Functions must always be declared on their own, not inside any functions. Your program must be laid out so they can be called, but never encountered, this is also why a function can't contain another function declaration (it would encounter the declaration):

invalid (both functions are encountered):


valid (neither function is encountered):
Roses In Dreams
16
Years of Service
User Offline
Joined: 16th Jan 2008
Location: Somewhere outthere.
Posted: 9th Feb 2008 15:26
Quote: "Not exactly what I meant"

Sorry for understanding it the wrong way. So the variables produced in functions are local and they disappear after the function is through.
Thank you for being soo helpful..

Im still absorbing some of the things here. I think I need practice to really get all this into my head.

Am I getting ahead of myself here?Im not sure. Please someone warn me.


Im not sure whether Im ready for the Dwords and memblock. They are not featured in TDK's tutorial maybe because they are not for noobs but im really getting curious with it. (dangerous curiousity bound for extreme failure)
i really appreciate the help.

Login to post a reply

Server time is: 2024-11-26 03:55:39
Your offset time is: 2024-11-26 03:55:39