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.

DarkBASIC Professional Discussion / Simple Ways to Optimize Code

Author
Message
PEH
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: usa
Posted: 2nd Sep 2009 21:31 Edited at: 2nd Sep 2009 21:39
I agree that is a better way. Sorry didn't quite think it all the way through.

Correct me if I am wrong but in that case wouldn't it be better to use gosub instead of functions? To me that isn't the purpose of functions because you don't need to pass any variables to it.

Quote: "The use of goto denotes a sloppy and unskilled programmer. Just because you can, doesn't mean you should."

I think it would be better to say that the use of a goto command in that way is sloppy. And in my opinion a gosub would have been better. Edit: with a return of course

Edit2:Hold I just searched for gosub and found there are allot of gosub vs func threads so let me read up on this. I might find the answer myself.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 2nd Sep 2009 21:40
Quote: "that isn't the purpose of functions because you don't need to pass any variables to it"


Yes, but the purpose of functions is not to pass variables in, it is to make the code reusable and modular, keeping variables local to that function away from other code.

You don't want the variable names in your menu code interfering with the variable names in your game code.

HavokDelta6
14
Years of Service
User Offline
Joined: 22nd Aug 2009
Location: United Kingdom
Posted: 2nd Sep 2009 21:46
Quote: "The use of goto denotes a sloppy and unskilled programmer"


Not if your doing like assembly code, where there are no loops

As the Wiki article also states

GCSE results:
2 a's 3 b's AND
5 (FIVE) a*'s ^_^
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 2nd Sep 2009 21:55
Quote: "The use of goto denotes a sloppy and unskilled programmer. Just because you can, doesn't mean you should."


I think it depends entirely on the context. I use GOTO occasionally to allow a function to have a cleanup routine inside itself, as it's usually the best way to make it self-contained.
PEH
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: usa
Posted: 2nd Sep 2009 22:04
After searching I have found that it seems to be that it is just user preference and some people think that goto's are unprofessional. I will keep that in mind when posting code but I personally am not afraid to use them in my own code. We should end this discussion and get back on topic.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Sep 2009 00:08
Quote: "and some people think that goto's are unprofessional"


And I'm one of them. Use of goto's rapidly makes code unreadable and I stopped using them about 25 years ago - except when I was using a language that didn't have satisfactory alternatives (there will now be a rash of posts from people who have found exceptions to this cast iron rule in my code ).

It can take a LONG time to unravel the logic of code containing goto's. I'd be delighted if Lee accidentally deleted it from the Compiler, Help and Keyword files in the next update.
Digger412
17
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 3rd Sep 2009 00:33
@GG & PEH & Benjamin & Mobiius - Goto has it's uses. It's like candy, it can be good depending on the taste, but should be used in moderation. Goto isn't a command that you want to use to do everything with. Occasional use is okay in my opinion, but it should not become a habit.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 3rd Sep 2009 01:29
There are times when 'goto' is useful. Just the same as there are other life forms in the universe. It's a billion to one that we're never going to meet them

Digger412
17
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 3rd Sep 2009 02:10
Okay, back on topic please. I'd like other members of the forum to post some of their techniques that they use to make their code more efficient.
veltro
User Banned
Posted: 3rd Sep 2009 12:29
The while loop is not an optimized code



using an if to exit a while loop?????

Better is
veltro
User Banned
Posted: 3rd Sep 2009 12:56
I want to underline another flaw in your benchmark code

You use
print "while endwhile:";timer()-t

and
print "do loop:";timer()-t

assuming that both print statement execute at the same speed. That's not true and is conceptually wrong, even if the difference is very irrelevant

Better you first get the execution time and then print it

s = timer()-t
print "do loop:";s
Digger412
17
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 3rd Sep 2009 23:55
@ veltro - Sorry, would have replied sooner but I had to go to school . Thanks, I did a bit of rewriting on each piece. I changed the timer() parts to match the rest of my examples, and I pulled the "if" out of the F/N loop, as well as the R/U and W/EW loops, since they are self-terminating. The difference is small, but they do run faster now. F/N still comes out on top, though.

sladeiw
15
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 4th Sep 2009 00:28 Edited at: 4th Sep 2009 00:28
You can emulate a loop with exit conditions with the following:



Add it into the previous tests to compare
Digger412
17
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 4th Sep 2009 01:20 Edited at: 4th Sep 2009 01:55
@sladeiw - I added it in, with the check it takes about twice as long to run, but it still is faster than the other types:



EDIT: made a little benchmark, F/N versus D/L (the progress interval is the span at which the loops will print their progress, don't set it too high because even though F/N runs BEST at a high, D/L just can't keep up with it, the autostop is to stop the D/L when it reaches the value that F/N reached):

Digger412
17
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 6th Sep 2009 20:00
Bump, come on! I know that people have more than just this! This is a community thread, if anyone has any optimizations that they can offer and validate, speak up please!

Login to post a reply

Server time is: 2024-06-16 15:45:38
Your offset time is: 2024-06-16 15:45:38