(This applies to Java and C, but the code is given in Python for readability.)

Is it faster to iterate over multiple separate arrays (tuples) of simple variables?

1 2 3 4 |
for i in range(0, n): phone = phones[i]; # ... email = emails[i]; |

Or over a single array of complex variables?

1 2 3 4 |
for i in range(0, n): phone = people[i].phone; # ... email = people[i].email; |

One array is faster than multiple arrays. This is because an array is stored in a contiguous block of memory. Accessing data in different arrays at the same time can require several different pages to be loaded from virtual memory. Memory access, especially hard drive access, is slow. As your application and data set grows, a significant performance difference may manifest itself.

**Arrays from the Second Dimension**

When iterating over a multidimensional array with indexes i and j, is it faster to iterate over j inside i?

1 2 3 |
for i in range(0, n): for j in range(0, m): cell = cells[i][j]; |

Or over i inside j?

1 2 3 |
for j in range(0, m): for i in range(0, n): cell = cells[i][j]; |

In Java and C, a multidimensional array[n][m] is stored as contiguous m-block of contiguous n-blocks. Let i be in n and j be in m. For a given i-cell, j-cells will be far apart. For a given j-cell, i-cells will be adjacent. Accessing adjacent values in memory is always faster.

For an array[i][j], putting j in the outer loop and i in the inner loop will significantly reduce potential virtual memory slowdowns.

This is the right way:

1 2 3 |
for j in range(0, m): for i in range(0, n): cell = cells[i][j]; |

The above only makes a difference with large data sets, but I like to cultivate good habits.