Optimizations

General FreeBASIC programming questions.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Optimizations

Postby Gonzo » Apr 06, 2011 22:13

I hope you guys can help me optimize a few parts of code that im unsure about

here is one example, that tells me if im at a border of a "sector"

Code: Select all

  If Not (bx = 0 Or bx = blocksz-1 Or by = 0 Or by = blockszy-1 Or bz = 0 Or bz = blocksz-1) Then GoTo nxt


this line is from my biggest bottleneck, and is part an iteration of every block in a sector to check for visibility
the line isnt executed unless theres a gain, its encapsulated by a byte hardsolid, which is enabled if the sector has solid block-edges all around
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Postby rolliebollocks » Apr 06, 2011 22:17

This should be faster:

Code: Select all

if bx <> 0 then
     if bx <> blocksz-1 then
         if by <> 0 then
             if by <> blockszy-1 then
                if bz <> 0 then
                    if bz <> blocksz-1 then
                        Goto nxt
                    endif
                endif
            endif
        endif
    endif
  endif
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Optimizations

Postby vdecampo » Apr 06, 2011 22:31

Gonzo wrote:I hope you guys can help me optimize a few parts of code that im unsure about

here is one example, that tells me if im at a border of a "sector"

Code: Select all

  If Not (bx = 0 Or bx = blocksz-1 Or by = 0 Or by = blockszy-1 Or bz = 0 Or bz = blocksz-1) Then GoTo nxt



I would try this...

Code: Select all

isZero = (bx * by * bz)
if isZero=0 then Goto Nxt
if bx = blocksz-1 OrElse by = blockszy-1 OrElse bz = blocksz-1 then Goto Nxt


-Vince
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Postby Gonzo » Apr 06, 2011 22:38

thanks rollie, it was a bit faster, however vdecampo, the multiplication will return true if only one of them is 0 :)
nice idea though
fxm
Posts: 9177
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Apr 06, 2011 22:50

Perhaps

Code: Select all

If bx <> 0 Andalso bx <> blocksz-1 Andalso by <> 0 Andalso by <> blockszy-1 Andalso bz <> 0 Andalso bz <> blocksz-1 Then GoTo nxt
 
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Apr 06, 2011 23:23

Gonzo wrote:thanks rollie, it was a bit faster, however vdecampo, the multiplication will return true if only one of them is 0 :)
nice idea though


Your IF statement said bx=0 or by=0 or bz=0 so x*y*z = 0 satisfies those conditions.

Unless I'm reading it wrong.

-Vince
Richard
Posts: 2955
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » Apr 06, 2011 23:43

@ vdecampo
If Not (bx = 0 Or ....
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Apr 06, 2011 23:51

So you set the if statement ...

If notzero then

Or

If iszero = 0 then

It works either way.

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

Postby rolliebollocks » Apr 07, 2011 0:38

I think it should be if isZero <> 0 then goto nxt because every condition has to be false inside NOT in order to return TRUE and goto nxt.

But the general logic of your test does not appear to be flawed. If any of the values are 0, then it should constitute "being on the boundary". Right?
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Apr 07, 2011 1:20

rolliebollocks wrote:I think it should be if isZero <> 0 then goto nxt because every condition has to be false inside NOT in order to return TRUE and goto nxt.

But the general logic of your test does not appear to be flawed. If any of the values are 0, then it should constitute "being on the boundary". Right?


Yes. The test for <> 0 is unnecessary because...

If x Then will only be true if x<>0

-Vince
Richard
Posts: 2955
Joined: Jan 15, 2007 20:44
Location: Australia

Postby Richard » Apr 07, 2011 2:30

Ideas;

Code: Select all

' If Not (bx = 0) Or (bx = blocksx-1) Or (by = 0) Or (by = blocksy-1) Or (bz = 0) Or (bz = blocksz-1) Then GoTo nxt

' precompute constants once to avoid the repeated decrement
blocksxm1 = blocksx - 1
blocksym1 = blocksy - 1
blockszm1 = blocksz - 1

' keep all "b"s grouped in expression so they can be in register optimised
If (blocksx - 1 - bx) * bx = 0 Then Goto NoGotoNxt
If (blocksy - 1 - by) * by = 0 Then Goto NoGotoNxt
If (blocksz - 1 - bz) * bz = 0 Then Goto NoGotoNxt
Goto nxt

NoGotoNxt:
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Apr 07, 2011 3:58

You can remove the IF altogether...

Maybe you can make the contents of your loop a macro, then call the macro 8 times (for the 8 edge cases -- 4 sides and 4 corners), You could slightly change the contents to fit the edge cases depending using macro parameters and #if...
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Postby rolliebollocks » Apr 07, 2011 4:14

@agamemnus

That doesn't make sense to me. Firstly, you are adding two checks to what was originally 6 checks, and secondly, #if cannot be used like that with numeric values.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Apr 07, 2011 22:59

rolliebollocks wrote:@agamemnus

That doesn't make sense to me. Firstly, you are adding two checks to what was originally 6 checks, and secondly, #if cannot be used like that with numeric values.


No, you're not adding any checks. You are removing all the checks It'd be kind of like converting this:
for i = 0 to 9
' do stuff
if i = 9 then
' do other stuff
end if
next i

to this:
for i = 0 to 8
dostuffmacro(null)
next i
dostuffmacro(dootherstuff)

You can definitely pass variables to macros... compile-time variables-- ie "null" or "dootherstuff".
Last edited by agamemnus on Apr 09, 2011 17:51, edited 1 time in total.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Postby rolliebollocks » Apr 07, 2011 23:08

Code: Select all

dim as integer yourewrong = 13

#if yourewrong = 13
    do:beep:loop
#endif
   


And the rest of what you said makes no sense. What did make sense, is wrong.

At any rate, the point of responding in a thread like this is to communicate clearly a possible solution.

for i = 0 to 9
' do stuff
if i = 9 then
' do other stuff
end if
next i

to this:
for i = 0 to 9
dostuffmacro(null)
next i
dostuffmacro(dootherstuff)


I don't understand what you can possibly mean by this. A macro will simply swap out one string of code for another. It only saves time when you are using it as opposed to a function.

How can you "remove all checks" and know if something is at the boundary?

Wtf?

Return to “General”

Who is online

Users browsing this forum: No registered users and 28 guests