Consider the following two functions:
fn copy() {
for a in 0..=1000 {
for b in 0..=1000 {
for c in 0..=1000 {
if a * a + b * b == c * c && a + b + c == 1000 {
let (a2, b2, c2) = (a, b, c);
println!("a:{} b:{} c:{}", a2, b2, c2);
}
}
}
}
}
fn no_copy() {
for a in 0..=1000 {
for b in 0..=1000 {
for c in 0..=1000 {
if a * a + b * b == c * c && a + b + c == 1000 {
println!("a:{} b:{} c:{}", a, b, c);
}
}
}
}
}
I would expect these functions to be the same when compiling with optimizations turned on, but they are not.
Currently on nightly copy is faster than no_copy. I speculate it is because println! acts like a black box for whether a, b and c will be mutated or not.
Godbolt
Playground to see the time difference
@rustbot label T-compiler I-slow C-enhancement
Consider the following two functions:
I would expect these functions to be the same when compiling with optimizations turned on, but they are not.
Currently on nightly
copyis faster thanno_copy. I speculate it is becauseprintln!acts like a black box for whethera,bandcwill be mutated or not.Godbolt
Playground to see the time difference
@rustbot label T-compiler I-slow C-enhancement