#include #include #include #include #define ARRAY_SIZE 1024 #define NUMBER_OF_TRIALS 1000000 double dtime() { double tseconds = 0.0; struct timeval my_t; gettimeofday(&my_t, NULL); tseconds = (double)(my_t.tv_sec + my_t.tv_usec * 1.0e-6); return (tseconds); } int get_model_name(char *mname) { FILE *fp; fp = fopen("/proc/cpuinfo", "r"); if (fp == NULL) { strcpy(mname, "(/proc/cpuinfo is not readable)\n"); return(1); } /* model name should be on the fifth line */ for (int i=0; i < 5; i++) fgets(mname, 80, fp); fclose(fp); return(0); } int main(int argc, char *argv[]) { /* Declare arrays small enough to stay in L1 cache. Assume the compiler aligns them correctly. */ double a[ARRAY_SIZE], b[ARRAY_SIZE], c[ARRAY_SIZE]; int i, t, rc; double m = 1.5, w1, w2, d = 0.0; char modelname[80]; /* Initialize a, b and c arrays */ for (i=0; i < ARRAY_SIZE; i++) { a[i] = 0.0; b[i] = i*1.0e-9; c[i] = i*0.5e-9; } /* Perform operations with arrays many, many times */ w1 = dtime(); for (t=0; t < NUMBER_OF_TRIALS; t++) { for (i=0; i < ARRAY_SIZE; i++) { a[i] += m*(m*b[i] + c[i]); } } w2 = dtime(); /* Print total time and processor type used in the run. Print a result so array ops aren't optimized away. */ for (i=0; i < ARRAY_SIZE; i++) d += a[i]; printf("d = %f time = %f\n", d, w2 - w1); rc = get_model_name(modelname); if (rc == 0) printf("%s", modelname); }