我正在研究 mandelbrot 算法来学习 Rust,我发现空的 25mil(大约 6k 图像)循环需要 0.5 秒。我发现它很慢。于是我去用python测试了一下,发现几乎花费了同样的时间。python的for循环真的是几乎零成本的抽象吗?这真的是我能用英特尔 i7 得到的最好的结果吗?
锈:
use std::time::Instant;
fn main() {
let before = Instant::now();
for i in 0..5000 {
for j in 0..5000 {}
}
println!("Elapsed time: {:.2?}", before.elapsed());
}
>>> Elapsed time: 406.90ms
Run Code Online (Sandbox Code Playgroud)
Python:
import time
s = time.time()
for i in range(5000):
for j in range(5000):
pass
print(time.time()-s)
>>> 0.5715351104736328
Run Code Online (Sandbox Code Playgroud)
更新:如果我使用初始化的元组而不是范围,python 甚至比 rust 更快 -> 0.33s
有没有办法在生锈中创建伪默认函数参数?我想做点什么
pub struct Circular<T> {
raw: Vec<T>,
current: u64
}
impl<T> Circular<T> {
pub fn new(t_raw: Vec<T>, t_current=0: u64) -> Circular<T> {
return Circular { raw: t_raw, current: t_current };
}
Run Code Online (Sandbox Code Playgroud)
我想选择设置current变量,但并不总是需要设置它.这是Rust可能做的事情吗?