Class MemoryTraffic

  extended byExamples.Array.MemoryTraffic

public class MemoryTraffic
extends java.lang.Object

Checks how the geometry of memory access in two dimensional arrays (row by row, column by column, random) impacts execution time. We allocate an M by N array with M=5,000 and N=1000 doubles for a total of 40MB. We then traverse the array in one of three ways incrementing each element by one:

Array elements will be accessed N*M times in each loop and execution will be timed.

Since the array is much bigger than the cache, memory traffic and the geometry of memory access plays an important role. The expectation is that 1. will run faster than 2. which will run faster than 3. To keep things fair we include two random number generator calls for each array access (one for each index) in all loops. We want to know how big the penalty is for jumping around in memory.

Results: the calls to the random number generator consume 95% of the time and yet row by row access is nearly twice as fast as 2. and 3. which are equally slow. Column by column access is just as bad as completely random access. Without random number generation row by row access is nearly 10 times faster than column by column access.

In the L2-cache: next we slim the arrays down to a size which fits into the L2-cache but loop through them more often so that the number of array element accesses remains the same. This should speed up things since it eliminates memory to cache traffic. The experiment is conducted only for row by row and column by column access, no random number generation.

Results: row by row access is 50% faster than column by column access and is about 2.5 times faster than the row by row access to main memory.

Constructor Summary
Method Summary
static void main(java.lang.String[] args)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public MemoryTraffic()
Method Detail


public static void main(java.lang.String[] args)