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 / For/Next loops quicker ?

Author
Message
psyvariar
21
Years of Service
User Offline
Joined: 16th Sep 2003
Location:
Posted: 8th Oct 2003 15:17
Still learning the basics here, one hting I'm wondering is whether putting For/Next loops into subroutines is detrimental to speed? Say I want to check the X positions of 50 3D cubes. Instead of gosub'ing to

checkx:
If Object Position X (1)> 100 then blah blah
If Object Position X (2)> 100 then blah blah
If Object Position X (3)> 100 then blah blah
return

would the routine
checkx:
For X=1 to 50 : If Object Position X (X)> 100 then blah blah : Next X
return

be quicker/slower at all ?
JamesBlond
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Germany
Posted: 8th Oct 2003 15:32
It is certainly better to use for/next loops. Imagine you had 50000 objects. Without the loops that would be a lot of typing to do there

Smile and be happy, things could be worse!
So I smiled and was happy, and things got worse...
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th Oct 2003 15:42
Using a loop to repeat your commands is slower ... but I bet you couldn't measure the difference

Really though, you are worrying about optimisation at the wrong end of your coding effort. Optimisation should always be the very last step you do because it generally complicates and disorganises your code. It's far harder to debug code once you've optimised it.
psyvariar
21
Years of Service
User Offline
Joined: 16th Sep 2003
Location:
Posted: 9th Oct 2003 12:10
Thanks for the tips. I've tinkered with Basic for years and have always avoided For/Next loops within the main program loop as it would have an obvious performance hit on early machines, but it seems to be quite common practice in DB coding.
skovron
21
Years of Service
User Offline
Joined: 14th Sep 2003
Location:
Posted: 9th Oct 2003 14:12
Its common practice not only in DB coding.
And optimization dont rely on avoiding loop. It relys on putting code (some calculations) wherever it is possible outside your loop
Refer to that what JamesBond said, there are much more advantages using loop than disadvantages
Guyon
21
Years of Service
User Offline
Joined: 6th Jun 2003
Location: United States
Posted: 9th Oct 2003 18:11
Optimization requires two main practices:

The first is know which way will do something is faster. For example using the shift operator is MUCH MUCH faster than multiplying or dividing. i.e 2 << 4 is 32. DB has no shift?

The second is reducing complex code into an equation. I had a discreet math class that dealt with turning problems in to equations. That I think IamM is talking about.
Get the code working then reduce it.
OSX Using Happy Dude
21
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 9th Oct 2003 19:11
DBPro certainly has shifts (I use them regularly) - dont know about DB 'Classic' though.

In addition, it helps if you do more things in a loop - that way the loop gets done quicker, eg :

Instead of :



You do something like :




Avatar & Logo by Indi. Insert witty comment here...
heartbone
21
Years of Service
User Offline
Joined: 9th Nov 2002
Location:
Posted: 9th Oct 2003 19:21
TheCodingArea your first snippet is illegal.

That Next may never get executed

The more you see, the more you know.
The more you know, the more you see.
Kelebrindae
21
Years of Service
User Offline
Joined: 15th Sep 2003
Location: Where cheeses are scarier than dragons.
Posted: 10th Oct 2003 12:41
For Guyon: Can you please explain me about "shifting" ? What is it and how does it work ?

For TheCodingArea: How do you use "shifts" in DBpro ? What syntax is required ?

Thanx

Ideas: memories of things which did not occur yet...
OSX Using Happy Dude
21
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 10th Oct 2003 13:25 Edited at: 10th Oct 2003 13:27
I know - copied it from the first post...

To use shifts, use

<< to multiply a value by a power of 2
>> to divide by a power of 2

1<<1 = 2
1<<2 = 4

etc


Avatar & Logo by Indi. Insert witty comment here...
Guyon
21
Years of Service
User Offline
Joined: 6th Jun 2003
Location: United States
Posted: 11th Oct 2003 08:23 Edited at: 11th Oct 2003 08:25
Sorry Kelebrindae,
TheCodingArea answered before I got to. ..and did it VERY well. SUPER JOB!!!!

Multipacation and division are VERY slow, so by shifting you are actually shifing the place of a biniany unit for example:

Binary 0010 = 2 Decimal
by shifing the 1 over a slot you double the number
so binary 0100 = 4 Decimal

Now shifting has a drawback of only working with integers. But you can see that moving a single 1 over a slot (or a few slots) is MUCH faster than doing the math especially for a computer that HATES base10

Off topic: Base10 is really a bad numbering system, the only reason we use it is because we have 10 fingers.
Math class over, don't push on the way out.

Login to post a reply

Server time is: 2024-09-21 04:15:55
Your offset time is: 2024-09-21 04:15:55