Is Rust faster than C?
steveklabnik.com
Is Rust faster than C?
1–10 of 402 posts
Re: Is Rust faster than C?
#2Re: Is Rust faster than C?
#3[0] tldr: "I think that there are so many variables that it is difficult to draw generalized conclusions."
Re: Is Rust faster than C?
#4also, if performance is critical to you, profile stuff and compare outputted assembly, more often than not you'll find that llvm just outputs the same thing in both cases
Re: Is Rust faster than C?
#5Re: Is Rust faster than C?
#6... well, that's what I get for reading an article with a silly title.
Re: Is Rust faster than C?
#7Re: Is Rust faster than C?
#8The article does not mention the possible additional optimisation opportunities that arise in Rust code due to stricter aliasing rules of references. But I don’t have an example in mind. Does anyone know of an example of it happening in real code?
Re: Is Rust faster than C?
#9In c the caller isn’t choosing typically. The author of some library or api decides this for you.
This turns out to be fairly significant in something like an embedded context where function pointers kill icache and rob cycles jumping through hoops. Say you want to bit bang a bus protocol using GPIO, in C with function pointers this adds maybe non trivial overhead and your abstraction is no longer (never was) free. Traits let the caller decide to monomorphize that code and get effectively register reads and writes inlined while still having an abstract interface to GPIO. This is excellent!
Re: Is Rust faster than C?
#10One example where Rust enables better and faster abstractions is traits. C you can do this with some ugly methods like macros and such but in Rust it’s not the implementers choice it’s the callers choice whether to use dynamic dispatch (function pointer table in C) or static dispatch (direct function calls!) In c the caller isn’t choosing typically. The author of some library or api decides this for you. This turns o…
Tbf this applies to Rust too. If the author writes
fn foo(bar: Box)
they have forced the caller into dynamic dispatch.Had they written
fn foo(bar: impl BarTrait)
the choice would've remained open to the caller