Optimizations

General FreeBASIC programming questions.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

You can't use #IF to compare integer values. The compiler will throw an error every time:

Code: Select all

#macro xyz( n )
    #if (n = 0)
        ? "hello"
    #endif
#endmacro

dim as integer z
xyz(z)
sleep
C:\FreeBasic\FBIDETEMP.bas(8) error 25: Invalid data types, found ')' in 'xyz(z)'
So that won't work.

The other part seems to involve replacing a 6 check routine with an 8 check routine.

I think your method may be drug related. That's ok. You make pretty maps.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

I am not using integer values...

http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgPpif

#define DEBUG_LEVEL 1
#IF (DEBUG_LEVEL >= 2)
' This line is not compiled since the expression is False
Print "Starting application"
#ENDIF

???
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

agamemnus wrote:I am not using integer values...

http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgPpif

#define DEBUG_LEVEL 1
#IF (DEBUG_LEVEL >= 2)
' This line is not compiled since the expression is False
Print "Starting application"
#ENDIF

???
Yeah, but... Everybody else in the thread is comparing integer values. That's the whole point of the thread. Usually I can make sense of the logic behind a technique, even when I don't understand the technique, or the wrong technique is being used, but in this case, I have no idea what can possibly be accomplished by using the preprocessor here.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

rolliebollocks wrote:Yeah, but... Everybody else in the thread is comparing integer values. That's the whole point of the thread. Usually I can make sense of the logic behind a technique, even when I don't understand the technique, or the wrong technique is being used, but in this case, I have no idea what can possibly be accomplished by using the preprocessor here.
There is no run-time comparison in my code because there doesn't need to be any run-time comparison. The #IF comparison is just to differentiate between the two different macro calls.

The idea is very simple... instead of going from i = 0 to 9 and checking if i = 0 or i = 9 every time, instead run the code for i = 0, then run the code for i = 1 through 8, and then run the code for i = 9.

Since all three pieces of code are going to be nearly identical, a macro with a parameter can be used to add the identical parts to the code and then add the non-identical parts.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

...And this has been public service announcement from the Partnership for a Drug Free America...
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

Post by DamageX »

rolliebollocks wrote:Yeah, but... Everybody else in the thread is comparing integer values. That's the whole point of the thread.
Well, OP only provided a small piece of code, out of context. There`s only so much optimization that can be done with something like that. I think that agamemnus was making a guess about the surrounding code and suggesting that the six conditionals may not need to be done for every iteration (and it sounds like this is probably correct). I`m not sure why he insists on macros however...
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

Well, OP only provided a small piece of code, out of context. There`s only so much optimization that can be done with something like that. I think that agamemnus was making a guess about the surrounding code and suggesting that the six conditionals may not need to be done for every iteration (and it sounds like this is probably correct). I`m not sure why he insists on macros however...
Macros are generally faster than say, functions/operators, so that part makes sense, but you can't get rid of the checks in this case. I won't conjecture on a conjecture about a conjecture but nothing about agamemnus' solution makes any sense to me (still).
suggesting that the six conditionals may not need to be done for every iteration (and it sounds like this is probably correct
...Except that agammenus' solution called for 8 conditionals... (the four sides and the four corners)...
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

The conditionals are only compile-time conditionals.

Gonzo posted that indeed the values, except for bx and by, are constant. Bx and by start at 0 and go to the maximum. There's no point in checking if bx equals 0 or if bx equals the max when you know that bx equals 0 at the start and it equals 0 at the end. Perhaps this is a better demonstration:

Code: Select all

dim i as integer

for i = 0 to 9
if i = 0 orelse i = 9 then print "At a limit.": continue for
print "Not at a limit."
next i

'VERSUS

i = 0
print "At a limit."
for i = 1 to 8
print "Not at a limit"
next i
i = 9 ' Redundant. For clarity...
print "At a limit"
The reason for macros is because the code inside Gonzo's for loop should not have to be repeated more than once... in the case of my extremely simple example, "At a limit" and "Not at a limit" would be encased inside a macro which would be called three times with one parameter. (indicating which phrase to put in)
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

It's a 3d space btw. You keep referring to it as if it's 2d.
The reason for macros is because the code inside Gonzo's for loop should not have to be repeated more than once...
I wouldn't speculate as to what should or should not happen in Gonzo's for loop. It may be that a simple tweak to the drawing routine would remove the need for any checks at all.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

rolliebollocks wrote:It's a 3d space btw. You keep referring to it as if it's 2d.
3D is the same as 2D or 1D in this case.

The reason for macros is because the code inside Gonzo's for loop should not have to be repeated more than once...
I wouldn't speculate as to what should or should not happen in Gonzo's for loop. It may be that a simple tweak to the drawing routine would remove the need for any checks at all.
LULZ. It's not speculation... I'm stating why I one would a macro -- so as not to repeat the same code over and over.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

The method of exhaustion. *yawn*. Ok, you win. It makes perfect sense somehow.
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Post by marcov »

agamemnus wrote:
The reason for macros is because the code inside Gonzo's for loop should not have to be repeated more than once...
It's like fixing a small bad with a big one :-)
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

rolliebollocks wrote:The method of exhaustion. *yawn*. Ok, you win. It makes perfect sense somehow.
:( I have run out of ways to explain it.. perhaps if Gonzo tries to implement it...



marcov wrote:
agamemnus wrote:
The reason for macros is because the code inside Gonzo's for loop should not have to be repeated more than once...
It's like fixing a small bad with a big one :-)
???? :\
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

I understand, now, what you're saying.

Code: Select all

dim i as integer

for i = 0 to 9
if i = 0 orelse i = 9 then print "At a limit.": continue for
print "Not at a limit."
next i

'VERSUS

i = 0
print "At a limit."
for i = 1 to 8
print "Not at a limit"
next i
i = 9 ' Redundant. For clarity...
print "At a limit"
The problem is that (bx, by) are not tile positions, but probably (speculating) belong to a game object of some kind, the for loop is probably constructed, to loop through every tile, to check if the player (or somethign) is at the border of the tile.

something like

for tx = 0 to tileMaxX-1
for ty = 0 to tileMaxY-1

That's why I think your solution doesn't make sense. Because he iterate the tiles anyways, in order to draw them, and likewise he needs to check each tile...

HOWEVER

It seems like the tiles are CONSTS, then they would all be in increments of some known amount, 16x8xwhatever...

So, one check might be able to be performed, to see if

if mod (bx/tileSizeX) = 0
if mod (by/tileSizeY) = 0
if mod (bz/tileSizeZ) = 0

Then, you wouldn't have to perform the check at all in loop...

That sounds right, I think. Maybe that'll work. I'm not sure. I wonder if Gonzo is even still reading this thread.

haha

Anyways, I sort of think I actually might know what you mean. The thing is, I read threads like this very closely, where people start posting alternate solutions, it gets interesting and you can learn a lot. Anyways, I spent at least 10 minutes for the past 3 days struggling to figure out what you meant (probably more, because I was so perplexed), I think we are understanding Gonzo's problem differently:
There's no point in checking if bx equals 0 or if bx equals the max when you know that bx equals 0 at the start and it equals 0 at the end.
I don't think bx is the counter, if it is then you're right, and it makes perfect sense.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

I'm glad you see what I'm saying now. I think we were just thinking differently in terms of how the blocks were iterated.

10 minutes? Is that 10 minutes per day, or 10 minutes total? :P
The problem is that (bx, by) are not tile positions, but probably (speculating) belong to a game object of some kind, the for loop is probably constructed, to loop through every tile, to check if the player (or somethign) is at the border of the tile.
Ah yes, it makes sense -- I would assume this too, if I were thinking about how to efficiently do this. In that case, my solution doesn't make sense! I kind of muddled it by using 2 dimensions as an example, as well. HOWEVER, on page 2, Gonzo posted that he indeed does iterate through from 0 to the max on all 3 coordinates.

I am not sure why he wouldn't just iterate through each object instead of each coordinate. There must be a reason...

Even if he were iterating through each object, one could remove the border test by marking each object as on the border, or off the border, as an attribute.
Post Reply