7 ms·
Performance. Rust is still twice as slow as C (http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=spectralnorm http://benchmarksgame.alioth.debia
by bjourne 10y ago
Performance. Rust is still twice as slow as C (http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=spectralnorm http://benchmarksgame.alioth.debian.org/u64q/performance.php...) which is still a fair bit slower than if a skilled assembly programmer had taken on the task.
Rust aficionados will say that their compiler is getting better, but so is C. clang has gotten faster than gcc on some benchmarks and on some others gcc has catched up and is now faster than clang again.
But what if you don't need optimal performance? Then you can use Rust. But then you can also use Go, Python, SBCL, Haskell, Java, C#...
- frankmcsherry 10y agoOr it is faster than C (http://benchmarksgame.alioth.debian.org/u64q/performance.php?test=mandelbrot http://benchmarksgame.alioth.debian.org/u64q/performance.php...). Depends which link you click on.
- bjourne 10y agoFrom the looks of it, that Rust program spawns 20 threads and does the computations in parallel. The C program does it all in one thread and doesn't even utilize sse intrinsics. I know full well that The Computer Language Benchmarks Game isn't a perfect source for programming language speed arguments, but what you can you do.
- tatterdemalion 10y agoYou're right - for microbenchmarks, the difference between Rust and C is going to be in how the solution is implemented, because the languages have such similar performance characteristics. This is why your original comment that Rust is not as performant as C is quite silly. You look rather hypocritical turning around and pointing it out when someone links to a microbenchmark on which Rust outperforms C.
- frankmcsherry 10y agoYeah, given that the CPU load for the C lines are listed as 100% 95% 95% 95% on a quadcore, I'm going to say "not one thread". Edit: to be helpful, rather than just obnoxious, (<3) the C version uses open mp pragmas, so it looks single-threaded.
- kbenson 10y agoWell, if you are familiar enough with the languages in question, you can look for what appear to be submissions that appear to use mostly idiomatic expressions of the language. Alternatively, if you are more interested in maximum possible performance, you can confirm both are making use of advanced techniques for quicker execution, but that's probably much harder to judge.
- Manishearth 10y agoSo it's okay to claim Rust is slower than C by cherry picking SIMD benchmarks (rust can do simd btw, just not on stable), but not okay to claim c is slower than rust by cherry picking a parallelization benchmark? If you don't think that benchmark is fair, submit a parallel solution in C. The benchmarks game is far from being even a useful source. It gives order of magnitude answers, and that's pretty much it. Using it (cherry-picked!) to back up a claim that Rust is 2x slower than C is disingenuous. "but what can you do" -- don't make absolute arguments about something using imprecise data. Actual rust in real world programs may actually end up being faster than C (see Yehuda Katz's talk on fast_blank in rust). C often needs to be hand-optimized. Rust, with its zero cost abstractions, often doesn't need to be; a naive program in rust would probably be faster than the same in C. Remember that fundamentally rust compiles the same way c does, and your rust code shouldn't have any more overhead. (except drop flags -- a minor cost -- which are something you might hand-implement in c anyway). We also use llvm, so we get mostly the same compiler optimizations.
- bjourne 10y agoI wrote "what can you do" because you are supposed to use some modicum of common sense when reading the numbers on The Benchmark Game. E.g in one of the benchmarks PHP beats both C and Rust, so you need to apply common sense to understand that that result is an outlier. I didn't cherry-pick; in 5/10 benchmarks, C is twice as fast as Rust. > rust code shouldn't have any more overhead. But it appears that it have. > We also use llvm, so we get mostly the same compiler optimizations. That is not a guarantee for efficient code. For example, in my testing, g++ is over 50% faster than clang++ in certain template-heavy scenarios.
- Rusky 10y agoNo, it does not appear that Rust has any overhead over C, except in cases that use SIMD. That is not a generally applicable result.
- nemothekid 10y ago>I didn't cherry-pick; in 5/10 benchmarks, C is twice as fast as Rust. Making the claim that C is twice as fast as Rust because of 5/10 benchmarks in the "benchmark game" shows an incredible lack of common sense to me. In 5/10 benchmarks, the benchmark games claims Go has equal if not better performance than Rust. Am I supposed to believe now, that a managed, garbage collected, 6-year-old compiler, language is as fast as as language without a runtime running on LLVM? Don't back up your claim with flawed benchmarks.
- dikaiosune 10y agoBoth examples are cherry-picked, is the point. Neither is particularly helpful in representing the whole story. That said, Rust makes it ridiculously easy to use parallelism if a task can support it, which is a strong advantage in its favor.
- xyience 10y ago> but what you can you do. Not make sweeping generalizations based on one benchmark you didn't write?
- nemothekid 10y agoI'm not sure thats the best source you can use. C is listed 5 times on there, the slowest taking 15 seconds. Likewise C #3 times similarly to Rust. So is the case that C is actually faster, or did the guy who wrote C #5 optimize the code and neglected to give the same optimizations to Rust.
- outworlder 10y agoThere's a pretty big chasm between "only 2x as slow as C" and "not needing optimal performance". That's even assuming it is generally true and a constant 2x factor. How many skilled assembly programmers you know that are able to write better code than the collective intelligence embedded in current compilers? Even if you have a few of them handy, then aren't their resources better spent in hand-optimizing the compiler output for critical sections only?
- valarauca1 10y agoThis difference on this test is caused by Rust not having stabilized SIMD support. Also Rust support hand rolled assembly (on nightly) that C has. On non-SIMD tasks Rust/C are neck and neck https://benchmarksgame.alioth.debian.org/u64q/rust.html https://benchmarksgame.alioth.debian.org/u64q/rust.html You're just cherry picking benchmarks. In the cases you care about raw number crunching power you'll likely be using a GPU not SIMD instructions as CPU's are roughly 3-4 orders of magnitude slower then GPU's at pure number crunching tasks. Not that SIMD isn't important as it's instructions also cover things like AES, SHA1/2, Random numbers, Cache pre-loading/evacuation, memory fences, and fast loading paths. But so few programmers worry about these things you are really hitting a niche market.
- quickben 10y agoGPUs aren't panacea. Such generalizations are wrong and will have you rearchitecture approaches after GPU io bottlenecks.
- valarauca1 10y ago>GPUs aren't panacea. Such generalizations are wrong This is true. But if you are doing hard number crunching you are using a GPU once you exhaust what a CPU can do. And most the time before you even touch SIMD as it's only a 4-8x speed up, while a GPU is 1000-10,000x. >will have you rearchitecture approaches after GPU io bottlenecks. 90% of these are caused by bad software. Either using legacy API's. Or by writing code that forces GPU's to talk to processor more often then is necessary. PCIe bandwidth is ~7.88GB/s [1] on modern Intel Chips (post 3xxx series). Compute GPU's often offer >10GB of on-board RAM. With last generations flag ships over a staggering 32GB of on-board RAM. And if you are hitting a wall with GPU compute/IO limits, CPU SIMD instructions aren't going to help you in the slightest. [1] http://www.tested.com/tech/457440-theoretical-vs-actual-bandwidth-pci-express-and-thunderbolt/ http://www.tested.com/tech/457440-theoretical-vs-actual-band...
- tbirdz 10y agoIt's not the bandwidth that's the problem, it's the latency. Many problems need more control flow and branching, and that is better done on the CPU. If you need to make decisions and take the previous iterations output as an input, then the overhead of transferring the data back and forth from cpu to gpu outweighs the benefit of the gpu speed. GPUs are good if you have an independent data parallel algorithm you are using to transform large blocks of floating point data. For other uses, CPU is better.
- Manishearth 10y agoMost of the slower benchmarks were fixed and just need to be merged iirc. I don't have much faith in microbenchmarks. Usually all they measure is how much effort the author put into overoptimizing code.
- mkawia 10y agoIsn't Rust's biggest advantage is safety , I make websites with golang , I can't stand the nil-ness and all the mutation mess I easily get myself into, could use a safer type system. Sometimes you just need safety and correctness .
- dpc_pw 10y agoRust can, somewhat is already and ultimately will beat C on performance, due to way better guarantees on pointer non-aliasing. See eg. http://stackoverflow.com/questions/146159/is-fortran-faster-than-c#146186 http://stackoverflow.com/questions/146159/is-fortran-faster-... Other than that Rust has way better zero-cost abstractions, so in practice allows writing faster code, as in C there are sanity limits after which you give up and write slower, but easier to manage code, as macro processor sucks, and type system is trying to stab you in the back at every step. Good example is `qsort`: http://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm http://www.tutorialspoint.com/c_standard_library/c_function_...
- Bromskloss 10y agoWhat are we looking for in the qsort example? Is there a Rust equivalent we should compare to?
- dbaupp 10y agoThe C code is forced to use a function pointer and hence do dynamic virtual calls for each comparison, while Rust and C++ can use generics/templates to get static dispatch (and hence inlining, constant folding etc.). You can see C vs. C++ in http://www.martin-ueding.de/en/programming/qsort/index.html http://www.martin-ueding.de/en/programming/qsort/index.html , and Rust is likely to be similar to C++.
- nwmcsween 10y agoLTO will allow qsort to be inlined, etc.
- tome 10y ago> Rust is still twice as slow as C which is still a fair bit slower than if a skilled assembly programmer had taken on the task. Really? Are there really people who write (a lot of) assembly in order to get code "a fair bit" faster than C? What on earth are they working on?
- bjourne 10y agoVM implementations and garbage collectors.
- bjourne 10y agoSince people were so angry I used The Benchmark Game as a source for benchmarks here (https://github.com/logicchains/LPATHBench/blob/master/writeup.md https://github.com/logicchains/LPATHBench/blob/master/writeu...) is another micro benchmark showing gcc & clang handily beating rustc. Though the timings are 1+1/2 year old. Their relative performances might have changed significantly.
- Manishearth 10y agoThat's pre-1.0. Rust has changed a lot. Also, given how prone microbenchmarks are to depending on hand-optimization over the compiler quality, benchmarks should have been contributed to by the community -- I don't think anyone in rust has heard about this one. Oh also, Rust is as fast as C/C++ there. It's just not faster than C++Cached, _which is a different algorithm_. That's the problem with microbenchmarks, you end up measuring differences in the algorithm used.
- bjourne 10y agoNo. In the first table Rust has 1874 and C++/clang 1722. The latter number is lower. C++ with clang beats Rust. In the second table all C and C++ versions beats Rust. 618, 749, 755 and 735 vs 877. That is a very big difference. You can also run the fucking benchmarks yourself and see for yourself. I have linked to lots of benchmarks showing C spanking Rust. None has shown any fair benchmarks were Rust is as fast as C.
- Manishearth 10y agoThat's a ... very small difference. And again, probably due to implementation differences. I'm not claiming C doesn't beat Rust, I'm just saying by very little -- Rust is practically just as fast, within the margin of error that microbenchmarks have. You have been belting out claims that Rust is 2x slower -- clearly false. Rust may be 5% slower -- which ... doesn't really matter. Look at wycats' talk on fast_blank. That's a real world example that's faster than C. Rust used to be faster than c on the regex benchmark at one point, as burntsushi pointed out.