TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Rust is now overall faster than C in benchmarks

696 pointsby wiineethover 4 years ago

40 comments

Matthias247over 4 years ago
Apart from those benchmark games a lot of real world C is a lot less performant than people think it might be. I spent a fair amount of time reviewing C code in the last 5 years - and things that pop up in nearly every review are costly string operations. Linear counts due to the use of null terminated strings and extra allocations for substrings to attach null terminators, or just deep copies because ownership can’t be determined are far more common than exceptional. This happens because null terminated strings feel like idiomatic C to most people.<p>Rust avoids those from the start by making slices idiomatic.<p>Another thing I commonly see is the usage of suboptimal containers (like arrays with linear search) - just because it’s there’s no better alternative at hand (standard library doesn’t offer ones and dependency management is messy). Which also makes it less surprising that code in higher level languages might perform better.
评论 #25626227 未加载
评论 #25625612 未加载
评论 #25625623 未加载
评论 #25625083 未加载
评论 #25627486 未加载
评论 #25627037 未加载
评论 #25627266 未加载
评论 #25639922 未加载
评论 #25626819 未加载
评论 #25641078 未加载
评论 #25631856 未加载
Svetlitskiover 4 years ago
Once LLVM fixes some bugs with `noalias`, at which point Rust will begin using it again in more circumstances [1], I&#x27;d expect to see Rust get even faster in these benchmarks, given that the Rust compiler knows <i>much</i> more about which pointers do&#x2F;do-not alias than most other programming languages [2] and the myriad optimizations this knowledge allows.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;issues&#x2F;54878#issuecomment-429578187" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;issues&#x2F;54878#issuecomment-...</a><p>[2] <a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nomicon&#x2F;aliasing.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nomicon&#x2F;aliasing.html</a>
评论 #25625278 未加载
评论 #25624745 未加载
FartyMcFarterover 4 years ago
Looking at the <i>reverse-complement</i> code, it appears that the Rust and C implementations are using different algorithms:<p><a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;program&#x2F;revcomp-rust-1.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a><p><a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;program&#x2F;revcomp-gcc-6.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a><p>On a quick inspection:<p>- The Rust code is about twice as long.<p>- The Rust code has CPU feature detection and SSE intrinsics, while the C code is more idiomatic.<p>- The lookup table is larger in the Rust code.
评论 #25624946 未加载
评论 #25625006 未加载
评论 #25624976 未加载
评论 #25641901 未加载
评论 #25625337 未加载
评论 #25625369 未加载
评论 #25625136 未加载
nynxover 4 years ago
The fastest n-body program is written in very idiomatic rust. <a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;program&#x2F;nbody-rust-8.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a>
评论 #25627780 未加载
评论 #25624780 未加载
seeekrover 4 years ago
From the page: &quot;… a pretty solid study on the boredom of performance-oriented software engineers grouped by programming language.&quot; I find this both funny and consider it true to some degree. There&#x27;s nothing like a good old friendly arms race for the benefit of all (languages and its users, in this case) involved!
harporoederover 4 years ago
I was wondering if perhaps this was actually measuring a difference between LLVM and GCC, but they also provide a set of benchmarks of C Clang vs C GCC (1) and Clang is generally slower in those test. Although there is some correlation between the ones Clang wins in C And Rust.<p>1. <a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;fastest&#x2F;c.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a>
评论 #25624471 未加载
评论 #25643346 未加载
lmilcinover 4 years ago
Which is not at all surprising. Rust has much larger compilation unit and knows more about what can read&#x2F;write a particular piece of memory. This allows some occasions for optimization where C compiler must be conservative.<p>An example of simpler version of this is Fortran that can be faster for numerical loads due to the fact that Fortran disallows aliasing of function arguments. C on the other hand, must pay the price of having to be conservative with how it treats arguments just in case they overlap.
评论 #25625557 未加载
olodusover 4 years ago
Yeah I can understand why. Though I still prefer C in some ways simply because of its minimalism in the language, while still allowing for the kinds of things you want to be able to do if you wanna push your code to the limits. It is a bit scary sometimes writing in C though and sometimes I get a bit annoyed at how some things work or not work in it. I personally am hoping Zig can soon fill this minimalism trait in langs for me. C will probably always be the standard though and I do think more programmers should learn C better to become better programmers.<p>Rust is a good lang though. I am glad something else is pushing up there for the top spots. And more competition in performance is a good thing.<p>You don&#x27;t always have to pick your sides. I just want to be able to write good code and be happy writing it :)
评论 #25627916 未加载
wuxbover 4 years ago
Just took a quick peek at the binary-trees C code. Why using openmp while the others don&#x27;t? why use recursive functions? The C implementation is not correctly optimized. People just paid more attention to Rust and other languages. Rust can be as fast as C, but &quot;faster&quot; is really misleading. BTW, I use clang all the time since it&#x27;s better than GCC.
评论 #25630023 未加载
评论 #25642095 未加载
ma2rtenover 4 years ago
I&#x27;m having a hard time making sense of this page. Why is this comparing fastest implementation with slowest implementation? Why is the metric busy time&#x2F;least busy? Why is C++ so much better than C?
评论 #25624845 未加载
infoseek12over 4 years ago
An unrelated rant about benchmarksgame. Has anyone noticed that the Python implementation of regex beats a lot of the Rust and C implementations? That’s because it uses the PCRE2 library (written in C) which it assumes is installed on the OS. Benchmarks are always artificial but this seems like a step too far: the benchmark hardly says anything about Python and is dependent on the OS environment having the right dependences.<p><a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;program&#x2F;regexredux-python3-2.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a>
评论 #25642266 未加载
评论 #25627160 未加载
indymikeover 4 years ago
Let&#x27;s talk about speed when we are implementing the same algorithm and optimizations, please. If $1 was donated to cure cancer every time a developer games a comparison like this, there would be no more cancer.
评论 #25626341 未加载
kzrdudeover 4 years ago
Rust seems to be using parallelism better. In one benchmark though (fasta), C gcc is using all 4 cpus and Rust only two, and still wins.<p>(Looking at just C gcc vs Rust) <a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;fastest&#x2F;gcc-rust.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a>
评论 #25624740 未加载
tutfbhufover 4 years ago
Nodejs is incredible fast for a interpreted language. It is only ~4 times slower than Rust and only a bit slower than Go or Java (compiled GC languages) in the benchmarks. Compare that with Python 3, also interpreted but ~30 times slower than Rust.<p>I know that Python 3 can do some runtime stuff that Nodejs can&#x27;t, but I wonder whether that&#x27;s worth so much performance. Maybe if the answer is that you would include C modules in Python if you need the speed, but I don&#x27;t know if that&#x27;s a good answer to the problem.
评论 #25626299 未加载
评论 #25633198 未加载
apiover 4 years ago
One of Rust&#x27;s performance advantages is the compiler&#x27;s ability to unambiguously determine memory aliasing. Aliasing is why many numeric kernels are written in Fortran, a much older language that also enforces strict aliasing as it simply doesn&#x27;t allow overlapping references.<p>There are probably others as well, but this is the advantage I&#x27;m familiar with. C&#x27;s ambiguity makes it harder to achieve some optimizations that can really matter on modern CPUs.
评论 #25624261 未加载
评论 #25624326 未加载
mh7over 4 years ago
Some of the rust versions calls C libraries for its heavy lifting (gmp, pcre) so I wouldn&#x27;t take this too seriously.
评论 #25627004 未加载
评论 #25628335 未加载
pmarinover 4 years ago
The only conclusion I have got about this web site is how much some programmers like to write benchmark code in Rust.
albertzeyerover 4 years ago
Interestingly, C++ seems to be the fastest overall.
评论 #25624056 未加载
moldaviover 4 years ago
Shouldnt they be about the same? (At least until the LLVM immutability optimizations happen)<p>I suspect surprising factors are in play.
评论 #25624280 未加载
评论 #25624894 未加载
1vuio0pswjnm7over 4 years ago
The reasons I use C versus comparable alternatives are not limited to speed. For example, the size of the compiler toolchain, the speed of compilation and the size of the resulting executables are all factors I have to consider. I do lots of work on systems with limited resources. How does Rust compare on those points versus, say, GCC.<p><a href="https:&#x2F;&#x2F;dev.to&#x2F;aakatev&#x2F;executable-size-rust-go-c-and-c-1bna" rel="nofollow">https:&#x2F;&#x2F;dev.to&#x2F;aakatev&#x2F;executable-size-rust-go-c-and-c-1bna</a>
评论 #25624424 未加载
haxoritoover 4 years ago
It’s all depends on developer. I have talent to write really bad code in any language and make code as slow as I want. I can write code in ruby that would outperform code in C. The benchmarks like this don’t give you full picture of language possibilities. On top of everything we also need account for compilers and compiler optimization. Nothing against rust. I write ton of code using rust, and advocating at my work. But this looks like a PR move to me.
topspinover 4 years ago
Is it just me or has that &#x27;benchmarks game&#x27; site been growing less navigable over time? It use to be easy to compare benchmarks across several languages. If that capability still exists somewhere it&#x27;s buried and I&#x27;m not interested in puzzling it out. There are no side bars or menus or anything helpful.
评论 #25627875 未加载
评论 #25652974 未加载
notoranditover 4 years ago
I am not sure I can buy such a comparison. Someone smarter than me already argued about test implementations. Someone else also put compilers and interpreters into prospective. Of course language expressiveness can gauge in but, IMHO, comparing the same sort algorithm or the same hash table implementation (or n-queens algo) could make much more sense especially with comparable compilers.<p>If Rust implementation is father than C&#x27;s, kudos goes to the compiler, not to the language
评论 #25626803 未加载
kowloover 4 years ago
Searched the page for &quot;Rust&quot; which returned nothing... and the box labels are awkward. Why not label them conventionally?
评论 #25624288 未加载
评论 #25653066 未加载
savant_penguinover 4 years ago
Wondering around the site I found this particular benchmark interesting<p><a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;performance&#x2F;nbody.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a><p>Why is julia using 250x the memory? (and it`s still fast)
评论 #25629533 未加载
batmansmkover 4 years ago
Was the Formula One pilot Schumacher as good as he is just because of his Ferrari? Obviously not, but he probably wouldn&#x27;t have performed as well with a Ford Focus.<p>If you are willing to spend the time to perform at the highest level, Rust can bring you there.
acjeover 4 years ago
It struck me a while ago that the most powerful feature of rust is the strong contracts libraries can and must express. This allows people with much deeper knowledge than me to make awesome stuff I can depend on.
anta40over 4 years ago
There are 2 charts on the page, both using the same title: &quot;How many times slower?&quot;<p>I don&#x27;t get those. What&#x27;s the difference?
评论 #25637301 未加载
dilapover 4 years ago
My experience using ripgrep and fd is that this is also true in real-world programs. :-)
srikuover 4 years ago
... but not as fast as C++&#x2F;g++ ?
评论 #25632003 未加载
tpoacherover 4 years ago
Define &#x27;c&#x27;.
layoutIfNeededover 4 years ago
C++ still seems to be the fastest.
JoeAltmaierover 4 years ago
Apples and oranges. How about Rust vs C++?
eeZah7Uxover 4 years ago
Thanks, I&#x27;d rather not use it. My time is valuable as well.
Animatsover 4 years ago
Two charts with different languages. Unclear what is being measured. Is this a humor article?
评论 #25625335 未加载
hvasilevover 4 years ago
How do I downvote a thread in HN, I can just upvote it?<p>Also is there also a way to filter out posts with the word &quot;Rust&quot; in it so I don&#x27;t see them?
评论 #25628853 未加载
rurbanover 4 years ago
Because Rust does alloca for all locals, and this if course faster. Everyone else avoids it for security reasons. Just search the Rust bugtracker for stack overflows.
评论 #25626550 未加载
etaioinshrdluover 4 years ago
I know it&#x27;s a meme, but it really does seem like most C or C++ code would be better off transitioning to Rust at some point. That includes the entire Linux kernel, web browsers, entire OS&#x27;s...
tpoacherover 4 years ago
Ah, rust. A language which combines the flexibility of assembly language with the power of assembly language.
joluxover 4 years ago
I think benchmarking C vs C++ vs Rust must only really be useful for researchers. They’re all making a similar tradeoff for performance: forcing you to consider how you use memory. Does anyone work in a field where the performance difference between these specific three platforms matters? I’m genuinely curious. Edit: also, if you could explain briefly why and what makes particular choices out of the three unsuitable, that would be awesome too.
评论 #25624835 未加载
评论 #25624478 未加载
评论 #25624269 未加载
评论 #25624375 未加载
评论 #25624196 未加载
评论 #25624279 未加载