我有两个(等效的?)程序,一个在 Go 中,另一个在 Rust 中。平均执行时间为:
- 去~169ms
- 生锈 ~201ms
去
package main
import (
"fmt"
"time"
)
func main() {
work := []float64{0.00, 1.00}
start := time.Now()
for i := 0; i < 100000000; i++ {
work[0], work[1] = work[1], work[0]
}
elapsed := time.Since(start)
fmt.Println("Execution time: ", elapsed)
}
锈
我编译了--release
use std::time::Instant;
fn main() {
let mut work: Vec<f64> = Vec::new();
work.push(0.00);
work.push(1.00);
let now = Instant::now();
for _x in 1..100000000 {
work.swap(0, 1);
}
let elapsed = now.elapsed();
println!("Execution time: {:?}", elapsed);
}
在这种情况下,Rust 的性能是否不如 Go?Rust 程序能否以惯用的方式编写,以更快地执行?