Jan 26, 2016

Whoa, that was fast! [Rust]

Browsing through Rust tutorial book's chapter Rust Inside Other Languages I was wandering, why the sample is running kind of slow (in non-release compilation): running a ten times slower than equivalent Delphi source.
Lets take a look at disassembled code for this fragment:
for _ in 0..5_000_000 {If you know that Rust's for is actually a syntax sugar for iterators, the result should no be so surprising:

The call to Iterator::next() is costly, it is far from as simple as "inc eax", that is why sample is running so slow.
One interesting note is what happening when you compile with --release switch. Lets take a look at our iterator loop: