Software - General
1758342 Members
1853 Online
108868 Solutions
New Discussion

Re: RUST’ED, GO’LANG WITH PYTHON

 
SOLVED
Go to solution
anandtk
HPE Pro

RUST’ED, GO’LANG WITH PYTHON

Python enthusiasts it’s for you!

This article targets developers, programmers, or individuals proficient in Python who have experience with related programming languages like Golang and Rust, developed by Google and Mozilla, respectively. Python, Golang, and Rust are widely recognized among developers for their advanced features such as parallelism, concurrency, efficiency, memory safety, and performance, which have contributed to their popularity within the programming community.

Scope:

The purpose of this article isn't to contrast the features, merits, and drawbacks of these languages. Rather, it serves as a quick glimpse into how developers can employ these programming languages to suit the needs of a particular project. The insights provided below stem from extensive programming expertise and understanding.

What is it about?

Python is simple and have huge collection of libraries and frameworks, whereas Golang and Rust have an ancestor called C and C++. 

Rust and Go programs extremely fast in comparison to interpreted languages such as Python.

More on Python:

Python, an open-source scripting language, enjoys widespread popularity and continuous growth due to its seamless integration with numerous applications and databases. It has become the top choice for students and beginner coders, thanks to its clear, concise syntax reminiscent of natural language. This simplicity makes Python significantly easier to learn compared to other programming languages.  

Its ease of comprehension is particularly crucial in the realm of Artificial Intelligence and Machine Learning (AI & ML), where programmers grapple with intricate algorithms and data manipulation. Python boasts an extensive ecosystem of libraries and frameworks across all platforms, including APIs, AI, and ML. These libraries offer ready-made functions and tools for swift and effortless execution of common tasks, featuring renowned ones like NumPy, Pandas, Scikit-learn, Boto3, TensorFlow, and PyTorch.  

Python's flexibility extends from basic tasks to the development of complex deep learning models, making it a versatile programming language.

Rust:

Rust, on the other hand, stands as a low-level, multi-paradigm programming language emphasizing safety and performance. Developed by Mozilla, Rust prioritizes execution speed by making specific design trade-offs. Its focus on safety is evident through features like the borrow checker, aimed at eliminating runtime bugs.  

Utilizing zero-cost abstractions and an ownership model, Rust ensures memory safety without relying on garbage collection. Its type system plays a crucial role in safety, eliminating null pointers to mitigate null reference errors—a significant advantage as null pointers are a notorious source of bugs.

Go:

Go is an open-source programming language that makes it easy to build simple, reliable, and efficient software. It is more concerned with simplicity. Go provides a superb built-in unit testing framework. Go has features like garbage collection, and automatic memory management which make it quick and easy to develop reliable, efficient programs.

Go’s explicit error checking uses the built-in error type where functions return errors as one of the return values. Go also has a built-in race detector that helps identify and fix race conditions in concurrent applications, contributing to its security profile. Race conditions occur when multiple threads access shared resources at the same instance, and one of the threads modifies the data.

Case Study:

The following case study was conducted to analyze the metrics and performance of various scripting languages. It involves a basic loop that iterates and prints to the terminal one million times. Additionally, the study records the duration of the process.  

It's important to acknowledge that the execution time may vary depending on the system configuration.

Rust Execution time: 1 minute 53 seconds

 

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);
}

 

 

Golang Execution time: 1 minute 44 seconds

 

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)
}

 

 

Python Execution time: 2 minutes

 

import time

i=0

starttime=time.time()
while i<=1000000:
    print(i)
    i=i+1
endtime=time.time()
execution_duration=endtime-starttime
print('execution_time',round(execution_duration)/60)

 

Comparing the execution time:   

table.jpg       

The above piece of program has been executed against these three scripting languages terminal and noticed that execution time of Go and Rust are better than Python.

 

Inter-programmability between scripting languages:

It means with in these three scripting languages, anyone can borrow modules or classes. Here are some of the examples.

python module in golang:

 

github.com/go-python/gpython

 

go packages in python:

 

github.com/go-python/gopy

 

Python embedded in Rust apps:

 

github.com/Rustpython/RustPython

 

Rust bindings for Python:

 

github.com/PyO3/pyo3

 

calling Rust code from Go:

 

github.com/mediremi/rust-plus-golang

 

 

Summary:

There may be a different way of writing a program to solve a problem using different programming languages. At the same time, the features, usability, popularity and accessibility of a particular programming language matters. As described earlier, its developer/programmer choice to choose the scripting language and shape up the solution.

About Author:

Anand Thirtha KorlahalliAnand Thirtha Korlahalli

Anand Thirtha Korlahalli works for "Infrastructure & Integration Services" domain as a Technology Architect, Remote Professional Services, HPE Operations – Services Experience Delivery, Bangalore

Thanks and regards,
Anand Thirtha Korlahalli
Infrastructure & Integration Services
Remote Professional Services
HPE Operations – Services Experience Delivery
I'm an HPE employee.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
2 REPLIES 2
Rustaceo
HPE Pro
Solution

Re: RUST’ED, GO’LANG WITH PYTHON

Hi, nice to read about Rust here, thanks for your post.

Only one thing about performance test. Rust println implementation is focused in memory efficiency instead of raw performance. It means that it doesn't use any kind of buffer (like Golang and Pyton do) to avoid memory allocations. Look, I have test your code and got:

no buffer

47 segs

Then I have added a buffer (like golang and pyton) to the code:

 

use std::io::stdin;
use std::time::Instant;
use std::io::stdout;
use std::io::BufWriter;
use std::mem::drop;
use std::io::Write;

fn main() {
    let lock = stdout().lock();
    let mut writer = BufWriter::new(lock);
    
    let start = Instant::now();
    for index in 0..1000000 {
        writer.write(index.to_string().as_bytes()).expect("STDOUT closed");
        writer.write(b"\n").expect("STDOUT closed");
    }

    drop(writer);
    let duration = start.elapsed();

    println!("Time: {:?}", duration);
    let mut input = String::new();
    stdin().read_line(&mut input).expect("Failed to read line");
}

 

and test it again:

buffer

7.6 seconds almost 7 times faster

In Rust this low level things have to be considered, it gives you much more control than mainstream languages.
This additional complexity gives you C++ like performance and efficiency, and the best for me, null safety and mandatory error handling (if u avoid unwrap), which is totally game changer in terms of software quality.

I'm an HPE employee.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
anandtk
HPE Pro

Re: RUST’ED, GO’LANG WITH PYTHON

Thanks @Rustaceo  for review and sharing your valuable feedback.

Thanks and regards,
Anand Thirtha Korlahalli
Infrastructure & Integration Services
Remote Professional Services
HPE Operations – Services Experience Delivery
I'm an HPE employee.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo