3 ms·
The author says "no magic here" for the C version: for (;;) printf("%s\n", argc>1? argv[1]: "y"); but it's not totally obvious to me whether the argumen
by Myrmornis 9y ago
The author says "no magic here" for the C version:
for (;;)
printf("%s\n", argc>1? argv[1]: "y");
but it's not totally obvious to me whether the argument to printf would be evaluated on every iteration of the for loop or not. Does the compiler know that those don't change, and is the answer to that question fairly basic C knowledge or not?
- pkaye 9y agoThe compiler can assume that argc will not change within the loop so it can optimize it. I just looked up the assembly output from gcc and it pulls the argc>1 outside of the loop and replaced the printf with puts. So something like: if argc>1 for (;;) puts(argv[1]) else for (;;) puts("y") The replacing of printf with puts is based on gcc having specific knowledge about the printf library function.
- ben_bai 9y agoThis is exactly the OpenBSD implementation.