4 ms·
Rust is neither faster nor more efficient than C++.... its benefits lie elsewhere.
by ComputerGuru 10y ago
Rust is neither faster nor more efficient than C++.... its benefits lie elsewhere.
- callahad 10y agoWhile literally true, Rust's benefits make it easier to write safe, parallel code, which often directly translate into greater speed and efficiency.
- barrkel 10y agoIt's more subtle than that. Different languages promote different idioms, in particular via their standard library. C++ is particularly unusual in that you don't get much out of the box, and in older projects even things like strings and containers are often custom-built. The combination of language and library provide affordances that make some idioms easier to represent, and others harder. This influences the design of code written in the language. Clumsy idioms generally don't get far, even if they would be more performant or provide some other benefits, like promote easier concurrency. And sometimes there's a tension: e.g. mutable structures tend to be more performant but harder to add parallelism to, and harder to reason about (leading to more copying than strictly necessary). Rust in particular has different ideas here. I think the jury is still out on whether Rust will in practice tend to promote code that is faster or slower than C++. Of course any given project in Rust could be rewritten in C++ to be just as fast, or perhaps faster. But that's not what happens in the real world. Human factors matter, path dependence matters, affordances matter.
- Cshelton 10y agoRust should be just as fast as C++ and C. Benchmarks are hard of course because optimizations and such. But if Rust is significantly slower, than it is a bug.
- ComputerGuru 10y agoI think you may have misunderstood my comment, as I did not mean to disparage Rust's speed nor imply it was slower than C/C++. Rust, at best, can match C/C++. But Rust cannot be faster than C/C++; therefore, by function of simply being rewritten in rust (and not because it was rewritten), that does not, in and of itself, make the resulting executables faster.
- steveklabnik 10y agoRust _can_ be faster than C or C++. The possibility is different than always, though.
- saosebastiao 10y agoThere is no proof that any existing C/C++ implementation can produce code that is globally optimal. I wouldn't say that it is impossible for Rust to beat C/C++...there is plenty of room for improvement in compiler optimization engines. I would even say that the room for improvement is potentially greater for rust than it is for C/C++, due to the type system. It hasn't been taken advantage of much yet, but MIR is one of the first steps to do so. It's an intermediate stage representation, like llvm's IR, but with all type resolution intact. There are plenty of known and even formally proven code optimizations that can only be used if certain guarantees are proven (such as the guarantees made by a strong type system).
- flukus 10y agoI hope it pans out and doesn't become the next "java will catch up to c one day".
- Manishearth 10y agoA big part of Rust is that it lets you very easily parallelize things, even in super complicated situations, because it keeps it safe. The corresponding task might certainly be possible in C++, but not necessarily always maintainable. Doing Servo's parallel styling or layout in C++ would be very hard. So it doesn't produce more efficient machine code, but it does open up new opportunities for optimization at a higher level.