Create a gist now

Instantly share code, notes, and snippets.

small memory access benchmark
#include <stdio.h>
#include <stdlib.h>
#define TEST_SIZE 10000000
long arr[TEST_SIZE];
void usage (char *name)
{
printf("USAGE: %s [forward|reverse|both]", name);
exit (1);
}
int main (int argc, char *argv[])
{
int i,j;
char mode;
if (argc != 2)
usage (argv[0]);
mode = argv[1][0];
if (mode != 'f' && mode != 'r' && mode != 'b')
usage (argv[0]);
for (j=0; j<100; j++) {
switch (mode) {
case 'f':
for (i=1; i<TEST_SIZE; i++)
arr[i-1] += arr[i];
break;
case 'r':
for (i=TEST_SIZE-1; i>=1; i--)
arr[i-1] += arr[i];
break;
case 'b':
for (i=TEST_SIZE-1; i>=1; i--)
arr[TEST_SIZE-i] += arr[i];
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment