Cache Lines, a Demonstration

Benchmark created by Sufian Rhazi on


Setup

var WIDTH = 2048;
  var HEIGHT = 2048;
  var array = new Array(WIDTH * HEIGHT);
  for (var row = 0; row < HEIGHT; ++row) {
    for (var col = 0; col < WIDTH; ++col) {
      array[row * WIDTH + col] = 0;
    }
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Cache-line happy
// For every row, increment every column item
// left-to-right, then top-to-bottom
// This causes linear access to the array, which makes your CPU cache happy.

for (var row = 0; row < HEIGHT; ++row) {
  for (var col = 0; col < WIDTH; ++col) {
    array[row * WIDTH + col] = 0;
  }
}
ready
Cache-line unhappy
// For every column, increment every row item
// top-to-bottom, then left-to-right
// This causes thrashed access to the array, which makes your CPU cache unhappy.


for (var col = 0; col < WIDTH; ++col) {
  for (var row = 0; row < HEIGHT; ++row) {
    array[row * WIDTH + col] = 0;
  }
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Sufian Rhazi on