Android Question Would this improve performance?

Discussion in 'Android Questions' started by wonder, Jun 5, 2015.

  1. wonder

    wonder Expert Licensed 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:
    Code:
    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:
    Code:
    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
     
  2. DonManfred

    DonManfred Expert Licensed User

    i dont think you will get a faster response with the changes
     
  3. wonder

    wonder Expert Licensed User

    Well, in the first example, you have 1792 cycles, the second example reduces this number to the largest array size which is 1024...
    So... maybe? o_O
     
  4. sorex

    sorex Expert Licensed User

    notice that the cases also (ab)use cycles for the checks.

    why not check the datetime ticks difference before and after that code?
     
    wonder likes this.
  5. wonder

    wonder Expert Licensed User

    I'll try that once I get home... :)
     
  6. sorex

    sorex Expert Licensed User

    the first one is 4.5 times faster because you leave out those checks.
     
    wonder likes this.
  7. wonder

    wonder Expert Licensed User

    So does that means that the IF / SELECT statement consumes more time than having more cycles?
     
  8. sorex

    sorex Expert Licensed User

    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
     
  9. thedesolatesoul

    thedesolatesoul Expert Licensed User

    The first code has 3 conditional checks, then second one has 4. (each for loop requires a condition check, and an incrementer)
     
    Peter Simpson and lemonisdead like this.
  10. sorex

    sorex Expert Licensed User

    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
     
  11. wonder

    wonder Expert Licensed User

    I understand. So, it's better to optimize my condition checks, rather than the number of cycles, right?
    By the way, what do you think it's faster? IF's or SELECT's?
     
  12. sorex

    sorex Expert Licensed User

    I think that a case is a comfort method and just gets converted to if/then/elseif's
     
  13. sorex

    sorex Expert Licensed User

    interesting...

    on 10000 run's a single case check appears to be 10 times slower than it's simple if/then alternative
     
    wonder likes this.
  14. sorex

    sorex Expert Licensed User

    with 2 checks (string based) the select case becomes 25x slower.
     
    RandomCoder and wonder like this.
  15. DonManfred

    DonManfred Expert Licensed User

    i would guess IFs
     
    wonder likes this.
  16. wonder

    wonder Expert Licensed User

    I'm actually learning a lot in here, guys! Keep it going!! :)
     
    Peter Simpson likes this.
  17. JordiCP

    JordiCP Well-Known Member Licensed User

    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
    Code:
    For i = 0 to (array_x.Lenght - 1)
     Whatever(array_x(i))
    Next
    I would try
    Code:
    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
     
    wonder likes this.
  18. sorex

    sorex Expert Licensed User

    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.
     
    wonder likes this.
  19. sorex

    sorex Expert Licensed User

    this seems to run at the same speed as the original tripple loop one and 17 times faster than the select/case.

    Code:
    Sub Example3
      
    Dim array_x(512As Int
      
    Dim array_y(1024As Int
      
    Dim array_z(256As 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
     
    wonder likes this.
  20. wonder

    wonder Expert Licensed User

    Great investigation, Sorex!! :)

    So the question remains...
    How to improve the original loop?
     
Loading...