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:
Expand Collapse Copy
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:
Expand Collapse Copy
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
 
notice that the cases also (ab)use cycles for the checks.

why not check the datetime ticks difference before and after that code?
 
Upvote 0
yes, but due to different reasons.

you need the max array size to cover 'm all
you have 3 if/then's
3 array size lookup's
3 value lookup


in the first that is minimize to 1 size and 1 value lookup
 
Upvote 0
first one is 17xx quick "runs"

second is 1024 slow ones

so you waste a lot on 768 & 512 lookup's that are not needed due to that max() in the for loop
 
Upvote 0
I think that a case is a comfort method and just gets converted to if/then/elseif's
 
Upvote 0
interesting...

on 10000 run's a single case check appears to be 10 times slower than it's simple if/then alternative
 
Upvote 0
I would choose to work "IF" or "DO WHILE"

Also, in a general IF-THEN-ELSE-...., I would choose the first condition as the more likely to be true (so the "else" condition will have to be evaluated less frequently)

Making a function call in an inner loop also has some overhead regardless of the method used. So, instead of
B4X:
Expand Collapse Copy
For i = 0 to (array_x.Lenght - 1)
 Whatever(array_x(i))
Next

I would try
B4X:
Expand Collapse Copy
Sub WhateverToAllArrayElements(Values() as int)
  for k=0 to Values.len-1 
     ...'Do Stuff ...
   next
End Sub

Sub Example
  ...
  WhateverToAllArrayElements(array_x)
  WhateverToAllArrayElements(array_y)
  WhateverToAllArrayElements(array_z)
  ...
end sub
 
Upvote 0
indeed, extra commands (jumps & returns) and stack operations.

not sure how much the reference passing eats tho but that's a single issue per sub call.

it all depends on that whatever sub what he's doing there.
 
Upvote 0
this seems to run at the same speed as the original tripple loop one and 17 times faster than the select/case.

B4X:
Expand Collapse Copy
Sub Example3
  Dim array_x(512) As Int
  Dim array_y(1024) As Int
  Dim array_z(256) As Int

  For i = 0 To (array_y.Length - 1)
     If i<511 Then
     Whatever(array_x(i))
  If i<255 Then Whatever(array_z(i))
     End If
     Whatever(array_y(i))
  Next
End Sub
 
Upvote 0
Back
Top