Go and rust Compute Performance Contest

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

As the go language fans have recently heard of the same famous modern language-rust, after reading the introduction, found that Rust is absolutely a very promising system-level language, so extra business time to learn.

There are quite a few contrasting articles on go and rust on the web, but from my point of view, there is no comparison between the two languages because their usage scenarios are basically not overlapping. Go is suitable for network programming, soft real-time system, rust is suitable for the memory use of harsh, non-GC, ultra-high performance scenes, so these two languages, if combined, is a very good pair of CP, you think?

Of course, Light said not dry bashi, on the way to rust, I will definitely go and rust for a lot of comparison and performance testing, here first a multi-threaded simple calculation of the comparison.


One, start 8 threads (Mac notebooks are 4 cores 8u), each thread performs 5 million calculation tasks, and then the statistics are time consuming.

First look at the code for Go:

Package Mainimport ("FMT" "Math/rand"//"Runtime" "Sync" "Time") Func main () {st: = time. Now () WG: = &sync. Waitgroup{}for I: = 0; I < 8; i++ {WG. ADD (1) go func () {//Prevent being optimized by the compiler, randomize the initial value x: = Int (rand. INT31 ()) for I: = 0; i < 5000000; i++ {//prevent compiler optimizations, randomization operation if (x+i)%2 = = 0 {x + = i} else {x = i}}fmt. PRINTLN (x) WG. Done ()} ()}wg. Wait () fmt. Println ("Time used:", time.) Now (). Sub (ST))}

Operation Result:

1298498081142713184720197278879399840593361225401474941318911902081140954425time used:  12.93551ms

Here's the Rust code:

extern Crate Time;extern Crate rand;use std::thread;use time::*; use RAND::RNG;FN main () {Let start = Time::now (); Let handles:vec<_> = (0..8). Map (|_| {Thread::spawn (| |                {//Prevent being optimized by the compiler, randomize the initial value let Mut rng = Rand::thread_rng ();                Let mut x = rng.gen::<i32> ();                Let num = 5_000_000; For I in 0..num {//Prevent being optimized by the compiler, Randomize if (x + i)% 2 = = 0 {x +                    = i;                    } else {x-= i;    }}). Collect (); For h in handles {println! (" Thread finished with count={} ", H.join (). Map_err (|_| "Could not join a thread!").    Unwrap ());    } Let end = Time::now ();    let duration = End-start; println! ("Time-consuming: {}", duration);}
Note here that we use the Realse Release mode: Cargo build--release

Thread finished with Count=1845603826thread finished with Count=496088980thread finished with Count=-1380406629thread Finished with Count=-360370413thread finished with Count=355179270thread finished with Count=119012173thread finished With Count=-229585086thread finished with count=-736809061 time: pt0.011544s
Conclusion : As you can see here, rust has a slightly higher performance than go, 11.5ms vs 12.9ms, with a simple calculation of 8 threads, and it doesn't have much advantage. So, without taking into account GC, memory control, generics and other factors, go is the first choice, because go learning curve than rust flat too much, development efficiency is also higher, not to mention the Go asynchronous network IO is so excellent, this aspect rust still need to improve ~

Add: When the number of threads increases further, for example 10,rust and go compare to 14ms vs 19ms, if 15 threads, that is 15ms vs 24ms;20 threads, 19ms vs 37ms;30 threads, 26ms vs 50ms. So it can be seen that, if the real-time computing system, that Rust's throughput for go still has a certain advantage, based on the thread and goroutine scheduling mode is still different.

Supplement 1:20 threads, each thread runs 5 billion times, rust and go time, 16.5s vs 36 seconds, memory consumption is not high, but can see that the occupation of go has been rising, Rust's memory footprint on the main thread, the calculation process memory will not increase, very stable. As for the CPU, there is no doubt 800%.

Advertising time

Welcome to join Golang Priory ,QQ group 894864, welcometo join this big family,Here's all you want toto the, and enthusiastic about the great God a lot Oh!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.