/* Last edited on 2013-10-02 03:50:32 by stolfilocal */ #include #include #include #include #include #include #define TYPE int32_t void test(int m, int n); #define int64_dec_fmt "ld" void test(int m, int n) { int j, i; double start; TYPE x = 31, y = 17; int64_t xx, yy; start = user_cpu_time_usec(); for (j = 0; j < n; j++) { for (i = 0; i < m; i++) { /* Ops to avoid the loop being optimized out of existence: */ x = y + i; y = x + j; } } double tare = user_cpu_time_usec() - start; /* Time for "empty" loop (us). */ xx = x; yy = y; fprintf(stderr, "x = %ld y = %ld\n", xx, yy); start = user_cpu_time_usec(); for (j = 0; j < n; j++) { for (i = 0; i < m; i++) { /* Ops to avoid the loop being optimized out of existence: */ x = y + i; y = x + j; /* Ops being timed: */ x = y + i; y = x + j; x = y + i; y = x + i; x = y + x; y = x + j; x = y + j; y = x + y; x = y + i; y = x + i; x = y + j; y = x + y; } } double toto = user_cpu_time_usec() - start; /* Time for loop with timed ops (us). */ xx = x; yy = y; fprintf(stderr, "x = %" int64_dec_fmt " y = %" int64_dec_fmt "\n", xx, yy); int nops = 12; /* Extra ops in inner loop 2. */ double ctop = ((double)n)*((double)m)*nops; /* Total number of ops executed. */ double tmop = (toto - tare)/ctop; /* Time per op (us) */ double vcpu = 1.0/tmop; /* Million ops per second. */ fprintf(stderr, "tare = %16.12f sec\n", tare/1e6); fprintf(stderr, "toto = %16.12f sec\n", toto/1e6); fprintf(stderr, "tmop = %16.12f usec/op\n", tmop); fprintf(stderr, "vcpu = %16.1f Mops/sec\n", vcpu); } int main(int argc, char **argv) { int m = 10000000; int n = 1000; fprintf(stderr, "start m = %d n = %d\n", m, n); test(m, n); fprintf(stderr, "stop\n"); return 0; }