17 ms·
Beating C with one line of Brainfuck
- jpgvm 7y agoI love how this blog only has this single post and it's dutifully tagged #shitpost. Thanks for sharing, I got a good chuckle out of it.
- klyrs 7y agoMarvelous. Anybody got an optimizing malbolge compiler?
- return_0e 7y agoNot an optimising one (yet) but a safe implementation of a malbolge interpreter written in Rust, which can run malbolge programs. Just for fun. https://github.com/return/malbolge https://github.com/return/malbolge
- lilyball 7y agoHas anyone used Rust to write an interpreter for INTERCAL yet?
- zentiggr 7y agoWash your mouth out with FROMs :)
- klyrs 7y agoYou didn't say PLEASE.
- shakna 7y agoOf course. [0] [0] https://github.com/birkenfeld/rick https://github.com/birkenfeld/rick
- lilyball 7y agoOh good. Though it's a shame that the test suite is a `test.py` file instead of `cargo test`; it would have been extremely satisfying to know that the Rust project would run the test suite for an INTERCAL compiler on a regular basis as part of Crater.
- citronvert2 7y agoÀ la première place, GG mec !
- deleted 7y ago[deleted]
- NilsIRL 7y agoGenerated C: #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; }
- markus_zhang 7y agomy eyes!
- zabzonk 7y agoNote that: #define OUTPUTS 0 char outputs[OUTPUTS] = { }; is not valid C (or C++) - zero length arrays are not a thing in either language,
- readingnews 7y agoI wonder if Brainfuck wins by his 100th of a second since he already called a program with words.txt, and therefore it was already loaded into memory, thus saving just a slice of time, as it would already be in the cache.
- re 7y agoCould also be that their version of wc is doing Unicode-aware counting by default; it could be a lot faster with LANG=C set in the environment. https://unix.stackexchange.com/a/96580 https://unix.stackexchange.com/a/96580
- kiwec 7y agoFor some reason, wc is even slower with LANG=C. 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.
- labawi 7y agoI prefer setting LC_ALL=C for to be sure, though LANG may be sufficient, as running "LANG=C locale" confirms LC_CTYPE=C is set.
- yitchelle 7y agoTo avoid the caching bias, I think a fairer test condition would be to use multiple files and do it over a number of times. The lowest average time would be the winner.
- sharedfrog 7y agoYou can manually clear the cache: `echo 3 | sudo tee /proc/sys/vm/drop_caches`
- klyrs 7y agoFor a #shitpost, one need only observe the fastest run of program A being faster than the slowest run of program B, to declare A is faster than B. With a sufficiently powerful static evaluator, you could embed the input into the program and get an even faster runtime.
- JoshMcguigan 7y agoIs the 85MB test file available? I wasn't able to find it. I'd like to test this result against nerve [0], a brainfuck to x86_64 asm compiler I wrote (like funkicrab, it is also written in Rust). [0]: https://github.com/JoshMcguigan/nerve https://github.com/JoshMcguigan/nerve
- apankrat 7y agoSame here. 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/ 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?raw=true https://github.com/dwyl/english-words/blob/master/words.txt?...
- kiwec 7y agoAfter editing my code to use EOF == 0, I get : 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'
- nipxx 7y agofile-cache and system load jitter. That's it.
- newnewpdro 7y agoRight, especially with there being only a single run of each, inconclusive.
- jacquesm 7y agoTotally missing the joke.
- yitchelle 7y agoThe HN crowd gets the joke, they just don't laugh about it. :-)
- jacquesm 7y agoHilarious. Hopefully this will end the endless flood of 'Beating C' posts. https://www.google.com/search?q=site%3Anews.ycombinator.com+beating+c https://www.google.com/search?q=site%3Anews.ycombinator.com+...
- rs23296008n1 7y agoBeating a C brainfuck compiler in rust/d/etc in 3 2 1...
- s_gourichon 7y agoGiven the humorous setup and experiment report, I would bet one second of my life that maybe the guy ran the test with a few random files passing by, and mentioned only the one that just happened to be slightly faster in brainfuck. I think I got the joke, it had me chuckle anyway.
- iRobbery 7y ago85 mbyte for 0.01s, on a terabyte you could easily get a cup a coffee or tea :)
- deleted 7y ago[deleted]
- stackzero 7y agoI want more articles like this.
- boothby 7y agoLook what you made me do! https://github.com/boothby/repiet/#but-is-it-faster-than-c https://github.com/boothby/repiet/#but-is-it-faster-than-c
- throw_m239339 7y ago> Following on the recent “faster than wc” blogposts, I decided to end this fad once and for all, using the best language ever created : Brainfuck. 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.
- lgeorget 7y agoThis blog post also does that actually since the Brainfuck code is transpiled to C.
- justinator 7y agoIt seems we've beat the webserver serving this blog with one hug from HN.
- andrewflnr 7y agoShould have written the server in brainfuck, I guess.
- paulddraper 7y agoThere must be way more people reading HN than I would think. 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.
- ddalex 7y agoThanks for the math! 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.
- spsful 7y agoCan someone summarize the article/link? I can't view it.
- ChrisSD 7y agoBrainfuck is faster than C. You should use it for all new code where performance is critical.
- gilbertmpanga12 7y agoHow about beating a whole C system with BrainCrap!
- stiray 7y agoI love it!!! 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.
- tutfbhuf 7y ago> The only question here is how good can the "compiler" be in making optimal cpu instructions from "your" 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.
- randomsearch 7y ago> Your language, that was made in c/c++, can hardly be faster than the language it was written in What if the language you wrote includes a run-time that does JIT?
- IcePic 7y agoand the JIT is written in...
- naikrovek 7y agoI am bemused by C and C++ fanatics who simply do not and apparently can not see beyond C and C++. Like it is impossible for society to surpass those languages, and that everyone will use them a million years from now... 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.
- judofyr 7y ago> Since cells need to be 32-bit for counting more than 255 words, you’ll also need to replace the few occurrences of char to int. 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?
- jangid 7y agoWill rust be any faster? Like 'ripgrep'.
- DarkCrusader2 7y agoHere you go. https://medium.com/@martinmroz/beating-c-with-120-lines-of-rust-wc-a0db679fe920 https://medium.com/@martinmroz/beating-c-with-120-lines-of-r...
- shultays 7y agoI think that BF implementation uses 32bit cells (which is not standard BF?)
- classified 7y agoIf you're going to "beat" C with another language, it will obviously be one that compiles to C.