TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Rust is now overall faster than C in benchmarks

696 点作者 wiineeth超过 4 年前

40 条评论

Matthias247超过 4 年前
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 未加载
Svetlitski超过 4 年前
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 未加载
FartyMcFarter超过 4 年前
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 未加载
nynx超过 4 年前
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 未加载
seeekr超过 4 年前
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!
harporoeder超过 4 年前
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 未加载
lmilcin超过 4 年前
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 未加载
olodus超过 4 年前
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 未加载
wuxb超过 4 年前
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 未加载
ma2rten超过 4 年前
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 未加载
infoseek12超过 4 年前
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 未加载
indymike超过 4 年前
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 未加载
kzrdude超过 4 年前
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 未加载
tutfbhuf超过 4 年前
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 未加载
api超过 4 年前
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 未加载
mh7超过 4 年前
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 未加载
pmarin超过 4 年前
The only conclusion I have got about this web site is how much some programmers like to write benchmark code in Rust.
albertzeyer超过 4 年前
Interestingly, C++ seems to be the fastest overall.
评论 #25624056 未加载
moldavi超过 4 年前
Shouldnt they be about the same? (At least until the LLVM immutability optimizations happen)<p>I suspect surprising factors are in play.
评论 #25624280 未加载
评论 #25624894 未加载
1vuio0pswjnm7超过 4 年前
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 未加载
haxorito超过 4 年前
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.
topspin超过 4 年前
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 未加载
notorandit超过 4 年前
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 未加载
kowlo超过 4 年前
Searched the page for &quot;Rust&quot; which returned nothing... and the box labels are awkward. Why not label them conventionally?
评论 #25624288 未加载
评论 #25653066 未加载
savant_penguin超过 4 年前
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 未加载
batmansmk超过 4 年前
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.
acje超过 4 年前
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.
anta40超过 4 年前
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 未加载
dilap超过 4 年前
My experience using ripgrep and fd is that this is also true in real-world programs. :-)
sriku超过 4 年前
... but not as fast as C++&#x2F;g++ ?
评论 #25632003 未加载
tpoacher超过 4 年前
Define &#x27;c&#x27;.
layoutIfNeeded超过 4 年前
C++ still seems to be the fastest.
JoeAltmaier超过 4 年前
Apples and oranges. How about Rust vs C++?
eeZah7Ux超过 4 年前
Thanks, I&#x27;d rather not use it. My time is valuable as well.
Animats超过 4 年前
Two charts with different languages. Unclear what is being measured. Is this a humor article?
评论 #25625335 未加载
hvasilev超过 4 年前
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 未加载
rurban超过 4 年前
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 未加载
etaioinshrdlu超过 4 年前
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...
tpoacher超过 4 年前
Ah, rust. A language which combines the flexibility of assembly language with the power of assembly language.
jolux超过 4 年前
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 未加载