Created
January 11, 2019 08:46
-
-
Save y2q-actionman/b32387a8e53030951e58a3fea86fcfa4 to your computer and use it in GitHub Desktop.
最速 fizzbuzz に sendfile や tee を使おうと思ったけど、端末にはどちらも使えず pipe で cat とかに繋ぐ必要があって残念
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
void __attribute__((noinline)) fizzbuzz_basic (void){ | |
int i; | |
for (i = 1; i <= 100; ++i) { | |
if (i % 15 == 0) { | |
printf("FizzBuzz\n"); | |
} else if (i % 3 == 0) { | |
printf("Fizz\n"); | |
} else if (i % 5 == 0) { | |
printf("Buzz\n"); | |
} else { | |
printf("%d\n", i); | |
} | |
} | |
} | |
int main (int argc, char* argv[]) { | |
int i, loop = 1; | |
if (argc > 1) { | |
loop = atoi(argv[1]); | |
} | |
fprintf(stderr, "Loops %d times\n", loop); | |
for (i = 0; i < loop; ++i) { | |
fizzbuzz_basic(); | |
} | |
return 0; | |
} |
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <limits.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
static const char FIZZBUZZ_STRING[] = | |
"1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz\n"; | |
void __attribute__((noinline)) fizzbuzz_pipe (void){ | |
int pipe_fds[2]; | |
int ret = pipe(pipe_fds); | |
if (ret < 0) { | |
perror("pipe"); abort(); | |
} | |
ret = write(pipe_fds[1], FIZZBUZZ_STRING, sizeof(FIZZBUZZ_STRING)); | |
if (ret < 0) { | |
perror("write"); abort(); | |
} else if (ret < sizeof(FIZZBUZZ_STRING)) { | |
fprintf(stderr, "too small\n"); abort(); | |
} | |
close(pipe_fds[1]); | |
/* char buf[16]; */ | |
/* read(pipe_fds[0], buf, sizeof(buf)); */ | |
/* write(STDOUT_FILENO, buf, sizeof(buf)); */ | |
ret = tee(pipe_fds[0], STDOUT_FILENO, INT_MAX, 0); | |
if (ret < 0) { | |
perror("tee"); abort(); | |
} else if (ret < sizeof(FIZZBUZZ_STRING)) { | |
fprintf(stderr, "too small\n"); abort(); | |
} | |
close(pipe_fds[0]); | |
} | |
int main (int argc, char* argv[]) { | |
int i, loop = 1; | |
if (argc > 1) { | |
loop = atoi(argv[1]); | |
} | |
fprintf(stderr, "Loops %d times\n", loop); | |
for (i = 0; i < loop; ++i) { | |
fizzbuzz_pipe(); | |
} | |
return 0; | |
} |
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/sendfile.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
const char FIZZBUZZ_FILE[] = "fizzbuzz.txt"; | |
const int FIZZBUZZ_FILE_SIZE = 413; | |
void __attribute__((noinline)) fizzbuzz_sendfile (void){ | |
int in_fd = open(FIZZBUZZ_FILE, O_RDONLY); | |
if (in_fd < 0) { | |
perror("open"); abort(); | |
} | |
/* char buf[16]; */ | |
/* int readcnt = read(in_fd, buf, sizeof(buf)); */ | |
/* int wrtcnt = write(STDOUT_FILENO, buf, sizeof(buf)); */ | |
int sent; | |
do { | |
sent = sendfile(STDOUT_FILENO, in_fd, NULL, 100); | |
if (sent < 0) { | |
perror("sendfile"); abort(); | |
} | |
} while (sent > 0); | |
close(in_fd); | |
} | |
int main (int argc, char* argv[]) { | |
int i, loop = 1; | |
if (argc > 1) { | |
loop = atoi(argv[1]); | |
} | |
fprintf(stderr, "Loops %d times\n", loop); | |
for (i = 0; i < loop; ++i) { | |
fizzbuzz_sendfile(); | |
} | |
return 0; | |
} |
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
static const char FIZZBUZZ_STRING[] = | |
"1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz\n"; | |
void __attribute__((noinline)) fizzbuzz_static_array (void){ | |
fputs(FIZZBUZZ_STRING, stdout); | |
} | |
int main (int argc, char* argv[]) { | |
int i, loop = 1; | |
if (argc > 1) { | |
loop = atoi(argv[1]); | |
} | |
fprintf(stderr, "Loops %d times\n", loop); | |
for (i = 0; i < loop; ++i) { | |
fizzbuzz_static_array(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment