// bench.c: times the validator on 64 MiB of ASCII and 64 MiB of mixed // 1- to 4-byte text, best of five runs each. // // build/bench.exe #include #include #include #include int utf8_valid_scalar(const uint8_t *buf, size_t len); static uint64_t rng = 0x2545F4914F6CDD1Dull; static uint32_t next(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)rng; } // One random code point: half of them ASCII, the rest spread over 2, 3 and 4 bytes. static size_t put(uint8_t *p) { uint32_t r = next(), c; switch (r % 6) { case 0: case 1: case 2: p[0] = 0x20 + (uint8_t)((r >> 8) % 95); return 1; case 3: c = 0x80 + (r >> 8) % 0x780; p[0] = (uint8_t)(0xC0 | c >> 6); p[1] = (uint8_t)(0x80 | (c & 63)); return 2; case 4: c = 0x800 + (r >> 8) % 0xF000; if (c >= 0xD800) c += 0x800; p[0] = (uint8_t)(0xE0 | c >> 12); p[1] = (uint8_t)(0x80 | (c >> 6 & 63)); p[2] = (uint8_t)(0x80 | (c & 63)); return 3; default: c = 0x10000 + (r >> 8) % 0x100000; p[0] = (uint8_t)(0xF0 | c >> 18); p[1] = (uint8_t)(0x80 | (c >> 12 & 63)); p[2] = (uint8_t)(0x80 | (c >> 6 & 63)); p[3] = (uint8_t)(0x80 | (c & 63)); return 4; } } static double now(void) { LARGE_INTEGER f, c; QueryPerformanceFrequency(&f); QueryPerformanceCounter(&c); return (double)c.QuadPart / (double)f.QuadPart; } static double best_of_five(int (*fn)(const uint8_t *, size_t), const uint8_t *buf, size_t n) { double best = 1e30; for (int i = 0; i < 5; i++) { double t = now(); if (!fn(buf, n)) { puts("valid input rejected"); exit(1); } t = now() - t; if (t < best) best = t; } return best; } static void race(const char *name, const uint8_t *buf, size_t n) { double scalar = best_of_five(utf8_valid_scalar, buf, n); printf("%.1f MiB of %s: scalar %.2f GB/s\n", n / 1048576.0, name, n / scalar / 1e9); } int main(void) { size_t size = (size_t)64 << 20, n = 0; uint8_t *text = malloc(size); if (!text) return 1; for (size_t i = 0; i < size; i++) text[i] = 0x20 + (uint8_t)(next() % 95); race("ASCII", text, size); while (n + 4 <= size) n += put(text + n); race("mixed text", text, n); free(text); return 0; }