0

Say I need to do two operations i times. Would it be better performance-wise to put both operations in a single for loop or create two separate for loops and but one operations in each?

Using two loops:

Sub Test()
    Dim i As Long

    Dim numbers() As Integer
    Dim numbersMultiplied() As Integer
    ReDim numbers(0 To 9) As Integer
    ReDim numbersMultiplied(0 To 9) As Integer

    For i = 0 To 9
        numbers(i) = i
    Next

    For i = 0 To 9
        numbersMultiplied(i) = numbers(i) * i
        Debug.Print numbersMultiplied(i)
    Next
End Sub

One loop:

Sub Test2()
    Dim i As Long

    Dim numbers() As Integer
    Dim numbersMultiplied() As Integer
    ReDim numbers(0 To 9) As Integer
    ReDim numbersMultiplied(0 To 9) As Integer

    For i = 0 To 9
        numbers(i) = i
        numbersMultiplied(i) = numbers(i) * i
        Debug.Print numbersMultiplied(i)
    Next
End Sub

Which one would be faster? Why? Or the performance of both ways is the same?

Andre
  • 26,751
  • 7
  • 36
  • 80
Ans
  • 1,212
  • 1
  • 23
  • 51
  • AFAIK, the more loops you have the more memory resources you are using, so by including them all in one cycle you are in essence reducing the resources needed to run your code. – Xabier Dec 22 '17 at 12:55
  • 5
    In real life it is extremely unlikely that you would be able to reliably tell the difference between your 2 tests even for large numbers of i – Charles Williams Dec 22 '17 at 13:00
  • 1
    https://stackoverflow.com/questions/198409/how-do-you-test-running-time-of-vba-code – braX Dec 22 '17 at 13:02
  • 4
    Sorry, there's no way to avoid the most annoying IT answer ever; *it depends*. If the loops iterate over the same objects in the same order then you could argue there's nothing to gain from a second loop. It's just more code to maintain. However, if the two operations are solving different problems it might make logical sense to split these (perhaps into separate functions). Generally speaking, I prefer code that is easy to read/grok over clever performance gains. How often is performance an issue? If you're not freezing the UI it's good right? – David Rushton Dec 22 '17 at 13:11
  • 1
    With the strength of PC's now a days, not sure performance really comes into it.. so it takes 6 seconds instead of 5.. does that really make much of a difference? Resource however does make a difference in my opinion. In your example above, single loop would be better just because it is more maintainable, easier to read and makes much better use of memory. Saying that, in your example, it really doesn't make too much difference.. I would say, performance is not really an issue – Zac Dec 22 '17 at 15:05
  • Using [Link](https://en.wikipedia.org/wiki/Big_O_notation) notation I think both run the same O(n) [link] – Moreno Dec 22 '17 at 15:10

1 Answers1

1

You can think about the fillowing parameters and assist your code

  • some CPUs contains hardware loops. This loop works as hardware cache for loop instructions but this requires that your loop don't exceed certain number of CPU instructions. Using hardware loops may increase your CPU performance.

  • Cache friendly code can increase the performance. In your code the single loop solution can make use of the value cached in this iteration - and it may be also in a register which is a more better thing - and this will reduce memory access time and enhance your performance

  • compiler optimization settings can also do things that you can't expect. In some cases the 2 loop solution may produce the same code as the single loop operation as the optimizer will detect that they are equivelant.

Those are only a few things to think about.