567 points · by pmontra · 7y ago · 86 comments
Thanks for sharing, I got a good chuckle out of it.
This should be mandatory for all "beating X in Y lines of Z" format posts.
#include <stdio.h>
char mem[30000] = { };
#define OUTPUTS 0
char outputs[OUTPUTS] = { };
int main() {
char* ptr = mem + 0;
fwrite(outputs, sizeof(char), OUTPUTS, stdout);
ptr[2] = getchar();
++ptr[2];
while (ptr[2]) {
ptr[2] -= 11;
while (ptr[2]) {
ptr[2] -= 22;
while (ptr[2]) {
++ptr[3];
--ptr[1];
while (ptr[1]) {
++*ptr;
++ptr[1];
}
ptr[2] = 0;
}
ptr[2] = 0;
}
ptr[1] = 0;
ptr[1] += ptr[3] * 1;
ptr[3] = 0;
ptr[2] = getchar();
++ptr[2];
}
ptr[1] = 0;
ptr[3] += *ptr * 1;
ptr[2] += *ptr * 1;
*ptr = 0;
*ptr += ptr[3] * 1;
ptr[3] = 0;
++ptr[1];
while (ptr[2]) {
--ptr[1];
while (ptr[2]) {
ptr[3] += 10;
while (ptr[2]) {
--ptr[2];
--ptr[3];
while (ptr[3]) {
++ptr[4];
ptr += 3;
}
while (ptr[4]) {
++ptr[4];
ptr[3] += ptr[4] * 1;
ptr[4] = 0;
++ptr[5];
ptr += 3;
}
ptr -= 3;
}
ptr[3] = 8;
ptr[2] += ptr[3] * 6;
ptr[3] = 0;
ptr[2] += ptr[4] * 1;
ptr[4] = 0;
ptr[3] += ptr[5] * 1;
ptr[5] = 0;
++ptr;
}
++ptr;
}
while (ptr[1]) {
--ptr[1];
ptr[3] += 8;
ptr[2] += ptr[3] * 6;
ptr[3] = 0;
ptr += 2;
}
while (ptr[0]) {
putchar(ptr[0]);
*ptr = 0;
--ptr;
}
ptr[1] += 10;
putchar(ptr[1]);
return 0;
} #define OUTPUTS 0
char outputs[OUTPUTS] = { };
is not valid C (or C++) - zero length arrays are not a thing in either language, (base) jam@jam-XPS-8500:~/svn/src/music/mp3tomidi$ cc -c -Wall x.c
(base) jam@jam-XPS-8500:~/svn/src/music/mp3tomidi$ cat x.c
#define OUTPUTS 0
char outputs[OUTPUTS] = { };
Looks like you're wrong about that; at least for GNU C. It's not official C but if it works and generates no warnings it will be used too.Long HN thread about this subject:
https://news.ycombinator.com/item?id=11674374
Zero length arrays are one of those things that C is renowned and notorious for: something that is useful at times but unpredictable because it is flirting with undefined behavior.
I usually compare C with a Formula 1 race car. It works well as long as you don't cut too many corners and when you do you end up plastered all over the road.
[neilb@Miranda temp]$ gcc -Wall -Wextra -pedantic a.c
a.c:2:6: warning: ISO C forbids zero-size array 'outputs' [-Wpedantic]
char outputs[OUTPUTS] = { };This describes all the stuff that makes C useful.
C sounds like one hell of a ride.
output probably contains all the output that doesn't depend on input (which this program doesn't have).
cat words.txt | time wc -w
13018291
0.76 user 0.01 system 0:00.78 elapsed
That's on the default wc build of void linux, and not in a proper benchmark environment, but that's still strange.With a sufficiently powerful static evaluator, you could embed the input into the program and get an even faster runtime.
If you feel the need to cheat, you could do something like I used for dumb_cat [0] and dump the resulting executables into a cache somewhere.
`echo 3 | sudo tee /proc/sys/vm/drop_caches`
That being said, in my experience those timings are not very accurate at this time scale; timing a program which takes just a few tenths to execute may report halving or doubling the user time between subsequent runs (and vice versa for systime).
bff [1] has no fancy assembly, not even a compiler, just some basic loop unrolling and jump precomputation, but I too wonder how it compares to wc.
[1] https://github.com/apankrat/bff/
-- edit --
Found words.txt with 466551 words, so replicating it 28 times gets it to the OP's test size. Bff is ~5x slower than 'wc -w' ... which is not unexpected, but still slightly disappointing.
https://github.com/dwyl/english-words/blob/master/words.txt?...
19.55 user 21.49 system 0:41.13 elapsed
However, you're using 8-bit cells, so the resulting word count is incorrect. In case you're wondering, the file was generated with : python3 -m faker -o words.txt text -r [large number] -s '\n'I think I got the joke, it had me chuckle anyway.
https://www.google.com/search?q=site%3Anews.ycombinator.com+...
Here we are 7 hours after post, overloading it.
Suppose "overloading" this blog is a mere 2 requests/s.
Suppose that an entire 10% of people reading HN click on this link, and on average click on it twice.
Further suppose that the average "active" HN user reads HN 5 times a week.
That means there are 1,210,000 active HN readers.
I did several estimations of How-Many-of-Us-are-Here and I got ~1M users by various methods (eg number of upvotes using Reddit engagement numbers), so I'll take your number with a happy confirmation bias.
This "Go faster than C" post was a complete joke, one could very easily write a "C faster than C" the exact same way to prove the absurdity of an optimized implementation that does not do what the original program does.
Well, part of the fun of Brainfuck is working around the limit of 8-bit cell size. How about creating a version which implements 32-bit numbers by storing them in four cells?
It is a great parody on latest trends in "my favorite xy language is faster than 'c/c++", each time I see one of those, it sends shivers down my spine.
Your language, that was made in c/c++, can hardly be faster than the language it was written in. Whatever optimizations it has, you can still make them in c/c++ with enough knowlidge, but probably you can optimize it some more (staring at c++ template metaprogramming) or use __asm or execute opcodes directly (cheating :D) The only question here is how good can the "compiler" be in making optimal cpu instructions from "your" language.
Stop this evangelist wars, your language can be great due to some other features (ease of use, knowlidge needed to be proficient in it, forgiveness of mistakes,..), you dont need to compare it to c/c++. It just doesnt make any sense.
What's funny is that C was initially written in B, and C smoked the living hell out of that language. As it turns out, the speed of compiled code and the running time performance of compilation is two drastically different things.
If C++ was a runtime language it would be slower than a checkout line at Walmart filled with grandma's trying to get in the last bit of Christmas shopping after all of them had just found out their kids are finally going to come out and bring their grand babies to visit for the first time after all these years.
In other words, the way you measure speed and the way I measure speed are to different things. If you had to account for the time it took to grow food back in the old days when accounting for how long it takes you to make dinner, you could say that standing behind a line of grandmas is way quicker.
But both realities are not only mutually exclusive but if you were to ask me; one of those two situations is more gratifying and the other is absolutely aggravating.
Waiting on C++ to compile feels like waiting on a line of grandmas at walmart just so I can get something to eat.
What if the language you wrote includes a run-time that does JIT?
#include <unistd.h> char code[] = "\x01\xb8\x00\x00\xbb\x00\x00\x2a\x00\x00\x80\xcd\x00";
int main(int argc, char argv) { int (func)(); func = (int ()()) code; (int)(*func)();
return 0; }
This whole sharade of how fast the language is a nonsense. There are other metrics that are more important TODAY, the industry needs languages that are maintainable, can be used by cheap workforce that is simple to find anywhere and dont need to know much about computers, memory,... but rather about a problem. Why picking on c/c++ which is not solving any of those problems is such a trendy topic today, is beyond my understanding.
No it won't. JIT-ed code can take into account dynamic information, and therefore outperform even the best general static compiler.
We will obviously still have them for a while, but anyone that thinks we have reached maturity in an industry that is around 50 years old is just not thinking objectively.
Its output is assembly code.
That assembly code can outperform code written in C, because it has access to dynamic run time information that your C compiler does not.
It doesn't work that way.
What if the purpose of the language is exactly to be faster than reasonably written C? Then a comparison makes sense.
Also, I have written a language that is "faster than C" in a certain naive sense, and it was not written in C. It does generate C, but not the kind of C sane humans like to write.
That does not mean those wc comparisons are indicative of real performance, though. They are just meant as provocative ways to get attention.
You're making the mistake of associating the performance of a program to be purely be a product of the language. It is not difficult to write inefficient code in any language.
> Stop this evangelist wars.
I think if there is any need for a war at all, then it should be between compilers not languages.