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 / Labels or functions?

Author
Message
dononeton
20
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 18th Jan 2007 02:09 Edited at: 18th Jan 2007 02:10
Well if I use this code as a function it runs alot slower

but if I use it as a label it runs alot faster I am making an error somewhere?

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 18th Jan 2007 17:50
No, you're not making an error. When you call a function, any non-global variables that you use within the function will be initialised automatically to zero - this takes a small amount of time for the processor to do, but most of the time, it's not easily measurable.

In your function code, you have 18 variables, so these will be cleared to zero each time. In your subroutine code, you also have 18 variables, but these will not be cleared each time. This may actually cause difficult to find bugs in your code, both because they are not being reset, but also because you may accidentally use those variable names elsewhere in your code.

It's a trade-off you need to make between safety and speed, between 2-3% slower code, or hours extra debugging code that looks perfectly OK but has a problem caused by something in a completely different part of the program. I know which I'd choose - debugging is usually harder than coding.

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 18th Jan 2007 18:33
You might also look at not using mouseclick() in the subroutine either. What you've done suggests to me that you are actually doing it in every subroutine.

I'm not quite sure what you are trying to do in that block of code, but why not read the mouse just once in the main loop?



That snippet probably won't work as you want it to by the way - it's just to demonstrate the principle. You can then use the Mx, My and Mc variables in all procedures in the same way (or pass them to functions).

It should then be a fair bit faster.

TDK_Man

dononeton
20
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 19th Jan 2007 00:59 Edited at: 19th Jan 2007 01:13
Thanks Guys I am still learning so I need to take my variables into consideration when I make my code. Some need to be local and others need to be global

If I make these variables global it works great

@ TDK_Man
Basically when you click on an object and hold the mouse button down you can move it
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 20th Jan 2007 01:38 Edited at: 20th Jan 2007 01:42
Quote: "Basically when you click on an object and hold the mouse button down you can move it"


Ah OK.

I did this a while ago in the attached program (if it's of any help to you).

TDK_Man

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-11-26 03:59:37
Your offset time is: 2024-11-26 03:59:37