========================================================================== /* This code creates a long cycle of length mem_size in an array of length mem_size */ for (i = 0; i < mem_size; i++) { perm[i] = i; } /* Product of random transpositions: (i, rand), i <= rand < mem_size */ for (i = mem_size - 2; i > 0; i--) { j = random(); j = j % (mem_size - i) + i; tmp = perm[j]; perm[j] = perm[i]; perm[i] = tmp; } for (i = 0; i < mem_size-1; i++) { /* Implicitly assume: array[i]=i+1; array[mem_size-1]=0; * Conjugate by random permutation, perm, implies following: * To save space, we could have initialized array[i]=i+1 * and then conjugated by random transpositions (i,rand) for i=0,1,2,... * and rand > i. */ array[perm[i]] = perm[i+1]; } array[perm[mem_size-1]] = perm[0]; ========================================================================== /* Once array[] has been created above, the code below is equivalent to making LOOP random accesses. */ for (i = 0, j = 0; i < LOOP; i++) j = array[j]; ========================================================================== /* The routine timing(), when called, returns the amount of time that has passed (in seconds) since the last call to timing() */ #include #include #include float timing() { static double time1 = 1e35; double time2, tmp; struct timeval tv; gettimeofday(&tv, NULL); time2 = tv.tv_sec + 1e-6 * tv.tv_usec; tmp = time2 - time1; time1 = time2; return tmp; } ========================================================================== // You can measure finer timings on an Intel Pentium with gcc. // The following sample code creates a function rdtscll(val) // that places the number of CPU clock ticks in a `long long' variable. // If anyone finds the corresponding assembly instruction for the // Sparc, please also report that to the class. // This fragment of code measures the CPU clock rate. Given this // information, and the number of clock ticks in a program interval, // you can measure very fine time units with high accuracy. // For a corresponding functionality for the Sparc, look up `man gethrtime' // (Sparc assembly also has a `rdtick' instruction, which may or may // not be privileged on our systems.) #ifdef __i386__ { # define rdtscll(val) \ __asm__ __volatile__ ("rdtsc" : "=A" (val)) long long val1, val2; rdtscll(val1); usleep((int)1e6); /* 1,000,000 usec = 1 s */ rdtscll(val2); printf("CPU speed: %1.2f GHz\n", (val2 - val1)*1e-9); /* ticks / 100 M */ } #endif