preface
When we choose a development language, we will consider all aspects of the characteristics, according to the actual needs of the appropriate choice. You can’t always have your cake and eat it, and to be efficient you must sacrifice performance and resource consumption, and vice versa. But Rust was a surprise! This article will share some popular science features of Rust programming language from five aspects: performance, memory safety, development efficiency, cross-platform and ecology.
The performance comparison
Different languages use different ways of managing memory. Some languages use garbage collection to find and release memory that is no longer used at run time, such as Java and Golang. In other languages, the programmer must allocate and free the memory himself, such as C/C++. Rust takes the third approach: memory is managed by an ownership system, which has a set of rules that the compiler checks at compile time, and no functionality of the ownership system incurs runtime overhead. Rust is fast and memory efficient. Standard Rust performance is comparable to standard C++ performance, and in some cases even higher than C++ performance. With no run-time and garbage collection, it can handle particularly performance-demanding services. There are plenty of articles on the web about Rust performance analysis and comparison, but to get a first-hand look, it’s better to do it yourself. I chose Python, C++, and Golang to compare performance with Rust.
Performance test scenario design
The same algorithm is implemented in four languages respectively, and The Times of completing tasks in the specified time are compared. The algorithm chosen for this test is to find all primes within 10000000 and compare The Times of finding all primes within one minute.
The binary size comparison generated after static compilation (or packaging)
Conclusion :(binary size) python > golang > rust > c++
Running speed comparison
In this scenario, compare the number of primes within 1,000,000 within 1 minute.
Conclusion: rust > c++ > golang > python
Here’s the thing: four tests on three different machines show that Rust is significantly more efficient than C++!!
Memory consumption comparison (rough calculation)
Conclusion: python > golang > rust > c++
CPU consumption comparison (rough calculation)
Golang > python > rust = c++
These are the results of my first test. When I saw the results, I was a little surprised that rust’s performance exceeded c++, which was unbelievable. So I searched online and found rust performance tests that had been completed by others.
I directly screenshot the conclusion:
Rust vs Golang.
Rust vs C++.
Conclusions: the above screenshots show that Rust is not only significantly better than Golang in terms of performance and resource consumption, but also comparable to C++ in performance and even better in some scenarios.
The above two test scenarios only test some simple algorithms. Next, we take a look at the comparison of performance resource usage in actual use. We still found a test report on the Internet, which implemented a Web backend using Python, PyPy, Go and Rust. Next, WRK was used to conduct pressure test on four HTTP servers. The test scenario was close to reality, and the conclusion was shown in screenshots directly:
Conclusion (Performance) : Rust still has significant performance advantages over Golang when used as a back-end service.
Conclusion (Resource occupancy) : Rust has an even greater advantage in memory occupancy, using only 1/3 of Golang’s.
Combined with the above three tests, Rust has a clear advantage in terms of operational efficiency and resource consumption. It is on the same level as C++ and far superior to Golang!
Rust development efficiency
As for Rust development efficiency, there is no unified objective evaluation standard, which basically depends on personal subjective feelings. Each person’s proficiency in different languages is also an important factor in development efficiency. About development efficiency, talk about personal feelings: Getting started with Rust is not as easy as getting started with languages like Python and Golang due to some of the strangest syntax in Rust (lifecycle tags being the most troublesome). However, because Rust exists primarily as a replacement for system languages like C/C++, it draws heavily on the syntax of C++. If you are familiar with C++, Rust is easy to get started; Second about development speed, for starters, Rust development experience like garbage classification when practiced in Shanghai Shanghai people of that kind of confusion and messy, the compiler checks is too strict, most of the time is to deal with the problem of compiling, a of of course written in other languages, is not in the Rust, but fortunately the compiler’s tips are very friendly, According to compile errors of most of the time to find the answer, but the compilation is expensive, but once compiled through, programmers don’t need to care about memory safety, such as memory leaks headache problem, only need to focus on business logic, wrote more than a month of Rust, number of debug are numbered, and each time the debug because business logic, Never debug code because of memory errors, crashes, etc. If you’re a little more comfortable with Rust, you’ll be as fast as Python and Golang, because at compile time Rust solves most of the problems and saves a lot of debug time.
Cross-platform sex
Like Golang, Rust is cross-platform and supports cross-compilation. A single piece of code can be compiled into binaries that run on Windows, Linux, ARM, MacOS, FreeBSD and other platforms. It is completely statically compiled and runs without relying on any third-party libraries. This feature is a boon for programmers who suffer from C++ cross-platform compilation. Rust is also supportive and friendly for embedded environments, and someone wrote a simple operating system in Rust.
Ecological problems
This is where Rust is weakest. As an upstart, its ecology is far less rich than Python and Golang, but some of the most commonly used libraries can be found; Moreover, Rust has been the most popular language on Stack Overflow for three consecutive years, attracting more and more attention. It is believed that Rust community will be richer and richer in the future.
The last
This chapter covers Rust performance comparisons, development efficiency, cross-platform, and ecology, but not memory security. Why?
Limited to the platform space reasons, too much about memory security, so I put in the next chapter, interested friends can continue to pay attention to, please look forward to!