DEV Community

Cover image for GO vs RUST speed test | Which one to choose in 2024
Mukesh Kuiry
Mukesh Kuiry

Posted on

GO vs RUST speed test | Which one to choose in 2024

TL;DR:

Go, developed by Google, is simple and readable, excelling in concurrency. Rust, from Mozilla, prioritizes memory safety and performance, making it great for systems programming. Go is simpler, Rust offers more control.

In performance, Go is efficient; Rust balances memory safety and performance.

According to Stack Overflow, 13.24% use Go, while 84.66% admire Rust.

Average salary: Golang - $92,760, Rust - $87,012.

Recommendation: Go is beginner-friendly, Rust for advanced control. Choose based on familiarity and project complexity.

Introduction

Go, developed by Google in 2007, emphasizes simplicity, efficiency, and readability. It excels in concurrent programming and scalability, with a straightforward syntax, making it popular for building distributed systems and web servers.

In contrast, Rust, introduced by **Mozilla** around 2010, prioritizes memory safety and performance. Ideal for systems programming, Rust's ownership system ensures memory safety without compromising performance, making it a powerful choice for building reliable and efficient systems.

Features

 
Go:

  1. Simplicity and Readability: Go is designed with a straightforward and readable syntax, prioritizing ease of understanding and code maintenance.
  2. Efficiency: Go achieves efficiency through a garbage collector, managing memory automatically to optimize resource utilization.
  3. Concurrency: Go stands out for its built-in support for concurrent programming, offering goroutines and channels that simplify the development of scalable applications.

Rust:

  1. Memory Safety: Rust places a robust emphasis on memory safety, incorporating features such as ownership and borrowing alongside a borrow checker to prevent common memory management errors.
  2. Performance: Rust focuses on performance with zero-cost abstractions and low-level control, making it suitable for high-performance applications without compromising safety.
  3. Fearless Concurrency: Rust provides a system for fearless concurrency, ensuring thread safety and empowering developers to write reliable and efficient concurrent code, particularly beneficial in systems programming scenarios.

In comparison, while Go excels in simplicity, readability, and efficient concurrency, Rust distinguishes itself with a strong commitment to memory safety, high performance, and a fearless approach to concurrency in the context of systems programming.

Performance

In the ever-evolving landscape of programming languages, developers often seek the right balance between simplicity, efficiency, and performance. In this exploration, we'll compare the performance of Go and Rust by measuring the time it takes to execute a seemingly simple task: printing numbers from 1 to 1 million.

Go: A Symphony of Simplicity and Concurrency



package main

import (
    "fmt"
    "time"
)

func main() {
    startTime := time.Now()

    for i := 1; i <= 1000000; i++ {
        fmt.Println(i)
    }

    duration := time.Since(startTime)
    fmt.Println("Execution time:", duration)
}


Enter fullscreen mode Exit fullscreen mode

Execution time: 240.3113184s

Rust: A Dance of Safety and Performance



use std::time::Instant;

fn main() {
    let start_time = Instant::now();

    for i in 1..=1000000 {
        println!("{}", i);
    }

    let duration = start_time.elapsed();
    println!("Execution time: {:?}", duration);
}


Enter fullscreen mode Exit fullscreen mode

Execution time: 313.9177696s

After running these programs separately, we observed the execution times. While the specific times may vary based on the environment and hardware, this simple experiment provides a glimpse into the relative performance of Go and Rust for a basic task.

In our test, Go demonstrated its efficiency in handling the task, leveraging its simplicity and built-in concurrency support. Rust, with its strong focus on memory safety and performance, showcased its capabilities even in a seemingly straightforward scenario.

It's important to note that performance considerations extend beyond a single task, and the choice between Go and Rust should be based on the specific requirements of your project. Whether you lean towards the simplicity of Go or the power of Rust, these languages offer unique advantages that cater to diverse programming needs.

Developer Opinion


In May 2023, more than 90,000 developers participated in Stack Overflow's annual survey, sharing insights into their learning methods, skill enhancement strategies, and tool preferences.

Among the 87,585 responses analyzed, 11,596 individuals, representing 13.24% of the respondents, reported using Go in the past year.

And, 11,429 participants, equivalent to 13.05%, indicated their usage of Rust over the same period.

Go and Rust uses

Apart from documenting the technologies developers utilized in the past year, the survey also delves into the tools currently in use and those developers aspire to continue using in the future.

According to the survey findings, Rust is highly favored, with 84.66% of developers expressing admiration for the language, and 30.56% expressing a desire to continue using it. Go, while still popular, shows slightly lower figures, with 62.45% admiration and 20.59% expressing a desire to use it further.

Admire

Admire

As per the survey results, the average salary for a Golang developer is reported to be $92,760, while Rust developers have an average salary of $87,012. These figures provide insights into the compensation trends within the respective developer communities, with Golang developers commanding a slightly higher average salary compared to their Rust counterparts.

Salary

Read the whole survey here -

Stack Overflow Developer Survey 2023

In May 2023 over 90,000 developers responded to our annual survey about how they learn and level up, which tools they're using, and which ones they want.

favicon survey.stackoverflow.co

My Recommendation

 
If you're new to coding, Go is a great pick with its simple and beginner-friendly features. Its clear syntax makes learning the basics a breeze, making it an ideal choice for those starting their programming journey.

On the flip side, if you're looking for a more advanced language that provides better control and performance, Rust is a compelling option. Despite a steeper learning curve, Rust's emphasis on memory safety and concurrency offers a powerful toolkit for experienced developers tackling complex projects.

Ultimately, the choice between Go and Rust hinges on your familiarity with coding and the complexity of the projects you aim to undertake. Go offers simplicity, while Rust provides a deeper understanding and greater control for those ready for a more advanced coding experience.

Follow for more such content. @mukeshkuiry :)

Do let me know your favorite programming language in the comment below.

Top comments (7)

Collapse
 
strottos profile image
Steven Trotter

Without meaning to be disrespectful, but I find these kinds of blog posts a bit pointless that compare loops of different programming languages. Practically, it means nothing when comparing performance of two programming languages.

In this case you're printing to the terminal 1 million times. All you're benchmarking is how fast your terminal was at that point in time. Terminals are generally pretty slow at this kind of thing.

If you pipe the output of each to /dev/null they'll be a lot faster each, like a lot faster. Example, the Rust program I ran once and got 8 seconds, then I changed the last line to print to stderr and ran again and it took 280ms.

I tried the go one once and it ran in 8 seconds, tried it again and the terminal froze froze for a bit and it took 24 seconds that time. This has absolutely nothing to do with the performance of Go.

For the record, once I took this out of the equation by piping the output to /dev/null they became more consistently and Rust was consistently a little bit faster, but not by anything that anyone cares about:

strotter:rust$ cargo run --release > /dev/null   # Optimizations On
    Finished release [optimized] target(s) in 0.00s
     Running `target/release/rust`
Execution time: 234.164995ms
strotter:rust$ cargo run --release > /dev/null
    Finished release [optimized] target(s) in 0.00s
     Running `target/release/rust`
Execution time: 231.208077ms
strotter:rust$ cargo run --release > /dev/null
    Finished release [optimized] target(s) in 0.00s
     Running `target/release/rust`
Execution time: 230.923915ms
strotter:rust$ cargo run --release > /dev/null
    Finished release [optimized] target(s) in 0.00s
     Running `target/release/rust`
Execution time: 229.28221ms
strotter:rust$ cargo run > /dev/null     # Optimizations off
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/rust`
Execution time: 240.099841ms
strotter:rust$ cargo run > /dev/null
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/rust`
Execution time: 241.410353ms
strotter:rust$ cargo run > /dev/null
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/rust`
Execution time: 240.907664ms
strotter:rust$ cargo run > /dev/null
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/rust`
Execution time: 244.846165ms
strotter:rust$ cargo run > /dev/null
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/rust`
Execution time: 241.136311ms
strotter:rust$ go run ../test.go >/dev/null
Execution time: 449.37522ms
strotter:rust$ go run ../test.go >/dev/null
Execution time: 482.430778ms
strotter:rust$ go run ../test.go >/dev/null
Execution time: 434.321493ms
strotter:rust$ go run ../test.go >/dev/null
Execution time: 442.613068ms
Enter fullscreen mode Exit fullscreen mode

Ultimately it doesn't matter anyway. Real programs are so much more complicated than this and unless you're comparing something that's semi-realistic it is a bit meaningless.

Collapse
 
strottos profile image
Steven Trotter

This being said I agree with your conclusion. Choosing based on familiarity, specifics of your needs and a variety of other things is likely much more important to you than performance if your choice is between Rust and Go, or if tweaking out optimal performance is highly important to you then you probably need to know a lot about the internals of each language.

Collapse
 
_coderdan profile image
Dan Draper

Rust’s memory model is also one of the reasons it’s so fast. It does not need a garbage collector (which can cause temporary drops in performance) and it can often avoid unnecessary copies of data.

While the memory safety properties of Rust provide improved security and reliability, these are not at odds with performance. In fact, quite the opposite!

Collapse
 
nothingimportant34 profile image
No Name

I had FORTRAN open while reading your text... and guess what? In addition to vintage vibes and classic reliability this benchmark loop completed in just 97 seconds! Granted, measuring execution time in seconds is bit finicky in FORTRAN for 2024, that is (that's why my result is INTEGER, thus less precise than yours) but its still almost 2.5x faster.

Collapse
 
der_gopher profile image
Alex Pliutau

Rust for Gophers with John Arundel packagemain.tech/p/rust-for-gophers

Collapse
 
der_gopher profile image
Alex Pliutau

We'll be publishing a post soon about comparing Rust and Go in 2024, stay tuned here - packagemain.tech/

Collapse
 
ikhwanperwira profile image
Muhammad Ikhwan Perwira

Here is rust is the winner based on your benchmark code:
github.com/ikhwanperwira/rust-go-b...