Android Question Would this improve performance?

wonder

Expert
Licensed User
Longtime User
Let's say you have a lot of For/Next cycles going on and you desperately need to improve your computing speed. What would you do?

I came up with this idea, reducing all the cycles into a single one.
Would this be a good technique?

Before:
B4X:
Sub Whatever(Value as int)
    ...
    'Do Stuff
    ...
End Sub

Sub Example
    Dim array_x(512)
    Dim array_y(1024)
    Dim array_z(256)

    Populate_Array(x)
    Populate_Array(y)
    Populate_Array(z)

    For i = 0 to (array_x.Lenght - 1)
        Whatever(array_x(i))
    Next

    For i = 0 to (array_y.Lenght - 1)
        Whatever(array_y(i))
    Next

    For i = 0 to (array_z.Lenght - 1)
        Whatever(array_z(i))
    Next
End Sub


After:
B4X:
Sub Whatever(Value as int)
    ...
    'Do Stuff
    ...
End Sub

Sub Example
    Dim array_x(512)
    Dim array_y(1024)
    Dim array_z(256)

    Populate_Array(x)
    Populate_Array(y)
    Populate_Array(z)

    For i = 0 to (Max(array_x.Lenght, Max(array_y.Lenght, array_z.Lenght)) - 1)
        Select True
            Case i < array_x.Lenght
                whatever(array_x(i))

            Case i < array_y.Lenght
                whatever(array_y(i))

            Case i < array_z.Lenght
                whatever(array_z(i))
        End Select
    Next
End Sub
 

sorex

Expert
Licensed User
Longtime User
it all depends what you are trying to do.

why are the sizes different? make them equal and you can leave out the checks that slow down things.
keep a counter that you don't need to go to the end if there are only 10 elements
use a list/map which is counter based already
...
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
it all depends what you are trying to do.

why are the sizes different? make them equal and you can leave out the checks that slow down things.
keep a counter that you don't need to go to the end if there are only 10 elements
use a list/map which is counter based already
...
This is a theoretical situation where there is no guarantee that the arrays will be the same size.
For example processing items which were read from a user generated database. Another example could be a physics engine, 256 galaxies, 512 suns, 1024 planets, all interacting through gravity. :)

I just want to find the most efficient way to optimize / write scalable code.
Thanks to you, we have already found that the SELECT method is way slower than an IF statement.
Pretty awesome piece of knowledge! :D I'll never use a SELECT in a cycle again.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
right, but you can't compare one case to another. I don't know how it ends up as java tokenized code and how it gets treatened by the interpreter.

if you look at PC 80x86 assembler an xor of a register with itself (for example xor al,al) is faster than using a plain zero fill mov al,0. Who would've thought that?

optimizing needs to be looked at case specific.
 
Upvote 0
Top