#include #include #ifdef __GNUC__ #include #include static inline double gettimeofday_sec() { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec + (double) tv.tv_usec * 1e-6; } #else #include static inline double gettimeofday_sec() { return clock() / double(CLOCKS_PER_SEC); } #endif template size_t NUM_OF_ARRAY(T (&)[N]) { return N; } int divloop(int offset, int step, int dividend, size_t count) { int ret = 0; int x = offset; for (size_t i = 0; i < count; i++) { ret += x / dividend; x += step; } return ret; } int main(int argc, char *argv[]) { const size_t count = 100000000; const int stepTbl[] = { 0, 1 }; const int offsetTbl[] = { 0, 1, 4, 17, 1025, 0x80000 }; const int dividendTbl[] = { 1, 17, 1024, 1029, 1234567 }; for (size_t j = 0; j < NUM_OF_ARRAY(offsetTbl); j++) { const int offset = offsetTbl[j]; for (size_t k = 0; k < NUM_OF_ARRAY(dividendTbl); k++) { const int dividend = dividendTbl[k]; for (size_t i = 0; i < NUM_OF_ARRAY(stepTbl); i++) { const int step = stepTbl[i]; double begin = gettimeofday_sec(); int ret = divloop(offset, step, dividend, count); double time = (gettimeofday_sec() - begin) / count * 1e9; printf("step %8d offset %8d dividend %8d %5.2f %8d\n", step, offset, dividend, time, ret); } } } return 0; }