static const struct printf_spec dummy_spec = { .type = FORMAT_TYPE_LONG_LONG, .flags = 0, .base = 10, .qualifier = 0, .field_width = 0, .precision = 0, }; #define REPS (4000) static unsigned measure_number(time_t* tp, unsigned long long num) { char buf[64]; time_t t, n; int i; unsigned count; t = *tp; count = 0; while (t == (n = time(NULL))) { for (i = REPS; --i >= 0;) number(buf, buf + 63, num, dummy_spec)[0] = '\0'; count += REPS; } *tp = n; return count; } static void measure() { time_t t, n; unsigned count1, count2, count3, count4, count5, count6, count7; t = time(NULL); while (t == (n = time(NULL))) continue; t = n; count1 = measure_number(&t, 8); count2 = measure_number(&t, 123); count3 = measure_number(&t, 123456); count4 = measure_number(&t, 12345678); count5 = measure_number(&t, 123456789); count6 = measure_number(&t, (1ULL << 32) - 1); count7 = measure_number(&t, (0ULL - 1)); printf("Conversions per second: 8:%d 123:%d 123456:%d 12345678:%d 123456789:%d 2^32:%d 2^64:%d\n", count1, count2, count3, count4, count5, count6, count7 ); } static void check(unsigned long long v) { char buf1[64]; char buf2[64]; number(buf1, buf1 + 63, v, dummy_spec)[0] = '\0'; sprintf(buf2, "%llu", v); if (strcmp(buf1, buf2) != 0) { printf("Error in formatting %llu:'%s'\n", v, buf1); exit(1); } } int main() { measure();measure(); measure();measure(); //measure();measure(); unsigned long long i; for (i = 0; ; i++) { check(i); // check(((1ULL << 32) - 1) + i); // check(((1ULL << 32) - 1) - i); check(0ULL - i); // check(i * i); // check(i * i * i); if (!(i & 0x3ffff)) { printf("\rTested %llu ", i); fflush(NULL); } } return 0; }