Search code examples
c#for-loopnested-loops

What is faster, nested loops or many loops in sequence?


I have a 2D array of size [3000,3] and I have to find Euclidean Distances between the 3000 values in first dimension 3 times (second dimension). What I am doing now is makin a nested for loop, I looked for ways of making it faster, but the only think I found was setting up a structure as here.

Perhaps doing 3 for loops is faster than doing a nested loop. Does anyone know how the processing time goes in this case?


Solution

  • It won't matter at all whether you run a loop three times via nested-loop or via separate loops, as long as the amount of iterations are the same.

    If you can improve your algorithm, so that you need fewer iterations (fewer than 3000 x 3), that might get you somewhere.