So if you read my last post, I enjoy performance testing. While messing around with Rust vs. Python, I noticed that the compiler might be taking shortcuts in Rust.
The code in question looped a number of times and added 1 to a variable; after the loop finished it printed out the variable.
a = 0
for i in range(150000000):
a++
print(a)
It seems simple, right? Well, not to Python… Python very obviously runs through every loop; all you have to do is run that little snippet. You can tell it is iterating through every loop because it doesn’t return immediately; in fact, it takes around four seconds to return on my machine.
If we take this same principle and write it in Rust, we get this:
fn main() { let mut a: u128 = 0; for _ in 0..150000000 { a += 1; } println!("{}", a);}
There are several ways to run Rust code; you can use “cargo run” for a faster compile time, or for performance you can use:
cargo run --release
This code does the same thing as Python, but Rust returns as close to instantly as you can get. The only…