动因:利用实际问题比较若干潜在可用于科学计算的编程语言的性能
用于比较的问题:自适应梯形定积分算法
待求积分表达式:
\int_0^\sqrt{8\pi} \sin(x^2)dx
编译方法:
C++: g++ -O3 -std=c++17
Rust: cargo build --release
scala: sbt compile
结论:
运行时间:Rust
代码行数:Scala
编码愉快度:Scala>Rust>>C++
打消了用scala写科学计算问题的念头,Rust的速度和C++几乎一样,大有潜力
运行时间详情:
循环计算100次
Rust:
time cargo run --release
...
cargo run --release 22.72s user 3.07s system 99% cpu 25.792 total
C++:
time ./a.out
...
./a.out 26.05s user 1.80s system 99% cpu 27.951 total
Scala:
time sbt run
...
[success] Total time: 168 s, completed Jan 8, 2019 11:42:52 AM
sbt run 452.16s user 1.77s system 258% cpu 2:55.73 total
代码见附件