Last active
October 29, 2021 21:31
-
-
Save clusterfudge/be110c9a9c277fd88b22f00dc758a7c9 to your computer and use it in GitHub Desktop.
Fast FizzBuzz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compiled with gcc main.cpp -O3 | |
#include<stdio.h> | |
#include<memory.h> | |
const int BUFFER_SIZE = 8092 * 4; | |
char BUFFER[BUFFER_SIZE]; | |
short BUFFER_INDEX = 0; | |
const int BIGINT_SIZE = 2048; | |
char BIGINT[BIGINT_SIZE]; | |
short BIGINT_INDEX = BIGINT_SIZE - 1; | |
short BIGINT_HEAD = BIGINT_SIZE - 1; | |
char FIZZ[5] = "fizz"; | |
char BUZZ[5] = "buzz"; | |
char FIZZBUZZ[9] = "fizzbuzz"; | |
void incr() { | |
BIGINT[BIGINT_INDEX] += 1; | |
if (BIGINT[BIGINT_INDEX] == ':') { | |
while (BIGINT[BIGINT_INDEX] == ':') { | |
BIGINT[BIGINT_INDEX] = '0'; | |
BIGINT_INDEX -= 1; | |
BIGINT[BIGINT_INDEX] += 1; | |
} | |
BIGINT_HEAD = BIGINT_INDEX < BIGINT_HEAD ? BIGINT_INDEX : BIGINT_HEAD; | |
BIGINT_INDEX = BIGINT_SIZE - 1; | |
} | |
} | |
void write_to_buffer(char * source, int start, int end) { | |
int len = end - start; | |
if (BUFFER_INDEX + len > BUFFER_SIZE - 2) { | |
BUFFER_INDEX = 0; | |
printf("%s", BUFFER); | |
// only wipe the maximum size we'll write to the buffer | |
// so we always have a terminator, but don't have to zero-out | |
int reset_size = 10 > BIGINT_SIZE - BIGINT_HEAD ? 10 : BIGINT_SIZE - BIGINT_HEAD; | |
memset(BUFFER + BUFFER_SIZE - reset_size, 0, reset_size); | |
} | |
memcpy(BUFFER + BUFFER_INDEX, source + start, len); | |
BUFFER_INDEX += len; | |
BUFFER[BUFFER_INDEX] = '\n'; | |
BUFFER_INDEX += 1; | |
} | |
int fizzbuzz_no_conditionals() { | |
// initialize BIGINT | |
memset(BIGINT, '0', BIGINT_SIZE); | |
BIGINT[BIGINT_SIZE - 1] = '\0'; | |
memset(BUFFER, 0, BUFFER_SIZE); | |
incr(); | |
while (true) { | |
write_to_buffer(BIGINT, BIGINT_HEAD, BIGINT_SIZE); | |
incr(); | |
write_to_buffer(BIGINT, BIGINT_HEAD, BIGINT_SIZE); | |
incr(); | |
write_to_buffer(FIZZ, 0, 4); | |
incr(); | |
write_to_buffer(BIGINT, BIGINT_HEAD, BIGINT_SIZE); | |
incr(); | |
write_to_buffer(BUZZ, 0, 4); | |
incr(); | |
write_to_buffer(FIZZ, 0, 4); | |
incr(); | |
write_to_buffer(BIGINT, BIGINT_HEAD, BIGINT_SIZE); | |
incr(); | |
write_to_buffer(BIGINT, BIGINT_HEAD, BIGINT_SIZE); | |
incr(); | |
write_to_buffer(FIZZ, 0, 4); | |
incr(); | |
write_to_buffer(BUZZ, 0, 4); | |
incr(); | |
write_to_buffer(BIGINT, BIGINT_HEAD, BIGINT_SIZE); | |
incr(); | |
write_to_buffer(FIZZ, 0, 4); | |
incr(); | |
write_to_buffer(BIGINT, BIGINT_HEAD, BIGINT_SIZE); | |
incr(); | |
write_to_buffer(BIGINT, BIGINT_HEAD, BIGINT_SIZE); | |
incr(); | |
write_to_buffer(FIZZBUZZ, 0, 8); | |
incr(); | |
} | |
return 0; | |
} | |
int main() { | |
fizzbuzz_no_conditionals(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
peaks at 2 GiB/s on a core i7-9850H