Rust mandelbrot 基准测试游戏编译

问题描述 投票:0回答:1

我正在 Rust 中实现基准游戏中的 mandelbrot C++ 代码 #8。我无法让它像 C++ 代码一样快地运行。我还尝试编译 mandelbrot rust 代码 #5,它的运行时间是预期时间的 6 倍。

我写的代码在这里:playground链接,即翻译后的代码,需要

cpu-time
,所以不能在操场上运行。

我的

Cargo.toml
文件如下所示:

[package]
name = "mandelbrot"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[profile.dev]
opt-level = 3

[dependencies]
cpu-time = "1.0.0"
rayon = "1.9"

我用

cargo run --release -- 16000

运行代码

问题是,我希望它的性能与 Mandelbrot C++ g++ #8 程序中的 C++ 代码类似,在我的计算机上需要大约 10 cpu 秒,而 rust 代码需要大约 17 秒。

我还尝试了 Mandelbrot Rust #5 程序,它需要超过 30 cpu 秒才能完成,在我看来这没有意义,因为网络上报告的旧 CPU 上的性能要好得多(~4 cpu -s)。

我做错了什么?

我也尝试通过注释掉相关行来限制 Rust 代码上的 I/O 操作,但没有任何改善。

编辑:

为了添加上下文,我的 C++ 代码是here (repl.it),并使用

g++ -fopenmp -O3 main.cpp -o mandelbrot.o
进行编译并使用
./mandelbrot.o 16000 out.pbm
运行。在我的电脑上,它的运行时间为 8.87 秒(显示的数字)。

对于 Mandelbrot Rust #5 程序,我使用的代码是来自网络的原始代码,带有一个带有

cargo new
的新货物项目,并将包添加到
Cargo.toml
文件中:

[package]
name = "rust_n5_2"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
generic-array = "1.0.0"
num-traits = "0.2.18"
numeric-array = "0.5.2"
rayon = "1.9"

我使用以下内容来计时使用

cargo build --release
构建的该项目的执行时间:

time ./target/release/mandelbrot 16000 > out.pbm

输出如下:

real    0m2.290s
user    0m35.564s
sys     0m0.070s

编辑2:

我使用了

RUSTFLAGS='-C target-cpu=native' cargo build --release
并让它运行得更快一点,大约 25 秒。我也尝试了 C++ 代码,使用
g++ -march=native -fopenmp -O3 mandelbrot.cpp -o mandelbrot.o
并将运行时间减少到 7.7 秒。

rust benchmarking mandelbrot rayon
1个回答
0
投票

让我们尝试使比较更简单。 我们来对比一下网站上的 Rust 程序:

$ ./rust-1.76.0/bin/rustc -C opt-level=3 -C target-cpu=ivybridge -C codegen-units=1 -L ./rust-libs --extern num_traits=./rust-libs/libnum_traits-73ccc110d6bb9d1b.rlib mandelbrot.rs -o mandelbrot.rust-5.rust_run

$ hyperfine "./mandelbrot.rust-5.rust_run 16000 >/dev/null"
Benchmark 1: ./mandelbrot.rust-5.rust_run 16000 >/dev/null
  Time (mean ± σ):      1.176 s ±  0.007 s    [User: 4.385 s, System: 0.066 s]
  Range (min … max):    1.168 s …  1.190 s    10 runs

:使用你的程序(没有 ProcessTime 的东西):

$ ./rust-1.76.0/bin/rustc -C opt-level=3 -C target-cpu=ivybridge -C codegen-units=1 -L ./rust-libs --extern num_traits=./rust-libs/libnum_traits-73ccc110d6bb9d1b.rlib mandelbrot.rs -o mandelbrot.rust-SO.rust_run

$ hyperfine "./mandelbrot.rust-SO.rust_run 16000 >/dev/null"
Benchmark 1: ./mandelbrot.rust-SO.rust_run 16000 >/dev/null
  Time (mean ± σ):      5.401 s ±  0.034 s    [User: 20.676 s, System: 0.113 s]
  Range (min … max):    5.378 s …  5.493 s    10 runs

这是一个很大的区别。

© www.soinside.com 2019 - 2024. All rights reserved.