1 /* 2 * from ftp://ftp.netbsd.org/pub/NetBSD/misc/gmcgarry/bench/memspeed.c 3 * 4 * Compilation: 5 * 6 * gcc -O3 -fomit-frame-pointer memspeed.c -o memspeed 7 */ 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <sys/times.h> 13 #include <time.h> 14 #include <sys/types.h> 15 16 #define KB (1024) 17 #define MB (KB*KB) 18 19 #define TESTSIZE (8*MB) 20 #define LOOPSIZE (256*MB) 21 22 int main(int argc, char *argv[]) 23 { 24 u_long mb = TESTSIZE; 25 u_long size, passes, d, i, j; 26 volatile u_long *mem; 27 struct tms tms; 28 time_t start, stop; 29 30 switch (argc) { 31 case 2: 32 mb = atol(argv[1])*MB; 33 case 1: 34 break; 35 36 default: 37 fprintf(stderr, "Usage: %s megabytes\n", argv[0]); 38 exit(1); 39 break; 40 } 41 42 mem = malloc(mb); 43 44 fprintf(stderr, "*** MEMORY WRITE PERFORMANCE (%d MB LOOP) ***\n", 45 LOOPSIZE/MB); 46 for (size = 64; size <= mb; size <<= 1) { 47 passes = LOOPSIZE/size; 48 fprintf(stderr, "size = %9ld bytes: ", size); 49 times(&tms); 50 start = tms.tms_utime; 51 for (i = 0; i < passes; i++) 52 for (j = 0; j < size/sizeof(u_long); j += 16) { 53 mem[j] = 0; mem[j+1] = 0; mem[j+2] = 0; mem[j+3] = 0; 54 mem[j+4] = 0; mem[j+5] = 0; mem[j+6] = 0; mem[j+7] = 0; 55 mem[j+8] = 0; mem[j+9] = 0; mem[j+10] = 0; mem[j+11] = 0; 56 mem[j+12] = 0; mem[j+13] = 0; mem[j+14] = 0; mem[j+15] = 0; 57 } 58 times(&tms); 59 stop = tms.tms_utime; 60 fprintf(stderr, "%5.3f MB/s\n", 61 (double)(LOOPSIZE/MB)/(double)(stop-start)*(double)CLK_TCK); 62 } 63 fprintf(stderr, "*** MEMORY READ PERFORMANCE (%d MB LOOP) ***\n", 64 LOOPSIZE/MB); 65 for (size = 64; size <= mb; size <<= 1) { 66 passes = LOOPSIZE/size; 67 fprintf(stderr, "size = %9ld bytes: ", size); 68 times(&tms); 69 start = tms.tms_utime; 70 for (i = 0; i < passes; i++) 71 for (j = 0; j < size/sizeof(u_long); j += 16) { 72 d = mem[j]; d = mem[j+1]; d = mem[j+2]; d = mem[j+3]; 73 d = mem[j+4]; d = mem[j+5]; d = mem[j+6]; d = mem[j+7]; 74 d = mem[j+8]; d = mem[j+9]; d = mem[j+10]; d = mem[j+11]; 75 d = mem[j+12]; d = mem[j+13]; d = mem[j+14]; d = mem[j+15]; 76 } 77 times(&tms); 78 stop = tms.tms_utime; 79 fprintf(stderr, "%5.3f MB/s\n", 80 (double)(LOOPSIZE/MB)/(double)(stop-start)*(double)CLK_TCK); 81 } 82 exit(0); 83 } 84