我在 Java 和 Rust 上运行了一个相同的小型基准测试。
爪哇:
public class Main {
private static final int NUM_ITERS = 100;
public static void main(String[] args) {
long tInit = System.nanoTime();
int c = 0;
for (int i = 0; i < NUM_ITERS; ++i) {
for (int j = 0; j < NUM_ITERS; ++j) {
for (int k = 0; k < NUM_ITERS; ++k) {
if (i*i + j*j == k*k) {
++c;
System.out.println(i + " " + j + " " + k); …Run Code Online (Sandbox Code Playgroud) 我有以下Rust程序(rustc 1.0.0-nightly(44a287e6e 2015-01-08 17:03:40 -0800)):
use std::io::BufferedReader;
use std::io::File;
fn main() {
let path = Path::new("nc.txt");
let mut file = BufferedReader::new(File::open(&path));
let lines: Vec<String> = file.lines().map(|x| x.unwrap()).collect();
println!("{}", lines[500]);
}
Run Code Online (Sandbox Code Playgroud)
根据http://doc.rust-lang.org/std/io/上的示例,上面是将文件的行拉成字符串向量的方法.我已经投入了第500行的输出.
为了解决Python中的相同任务,我写了以下内容:
#!/usr/local/bin/python3
def main():
with open('nc.txt', 'r') as nc:
lines = nc.read().split('\n')
print("{}".format(lines[500]))
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
当我运行编译的Rust并计时时,我得到了这个:
rts@testbed $ time ./test
A declaration of independence by Kosovo will likely bring a similar declaration from Georgia's breakaway Abkhazia region, which Russia could well recognize.
./test 1.09s …Run Code Online (Sandbox Code Playgroud) 我有一个程序,对于所有小于或等于输入的整数,找到可以表示为两个立方之和的数字,两次,又名拉马努金数字问题。
我用 Java 和 Rust 编写了这个,但是,它在 Rust 中的运行速度比 Java 慢两倍多。
我能做些什么来让它表现得更好,或者以其他方式改进它吗?
铁锈代码:
use num_integer::Roots;
fn main() {
let v = 984067;
// let v = 87539319;
for i in 1..=v {
ramanujan(i)
}
}
fn ramanujan(m: i32) {
let maxcube = m.cbrt();
let mut res1 = 0;
let mut res2 = 0;
let mut _res3 = 0;
let mut _res4 = 0;
for i in 1..=maxcube {
for j in 1..=maxcube {
if i * i * i + j …Run Code Online (Sandbox Code Playgroud) 我正在研究 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
我是Rust的新手,我只是想通过从文本文件中执行基本的逐行读取来熟悉io库.我试图编译的例子直接来自网站.
use std::io::BufferedReader;
use std::io::File;
fn main() {
let path = Path::new("file_test.txt");
let mut file = BufferedReader::new(File::open(&path));
for line in file.lines() {
print!("{}", line.unwrap());
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用rustc编译它时,这些是我收到的错误:
io_test.rs:1:5: 1:28 error: unresolved import `std::io::BufferedReader`. There is no `BufferedReader` in `std::io`
io_test.rs:1 use std::io::BufferedReader;
^~~~~~~~~~~~~~~~~~~~~~~
io_test.rs:2:5: 2:18 error: unresolved import `std::io::File`. There is no `File` in `std::io`
io_test.rs:2 use std::io::File;
^~~~~~~~~~~~~
error: aborting due to 2 previous errors
Run Code Online (Sandbox Code Playgroud)
我正在使用Ubuntu 14.04,我不知道这是否是问题的一部分.我非常感谢任何帮助.也许这只是我的一些简单错误或错误.
我目前正在学习 Rust,因为我想在需要非常高性能的项目中使用它。我最初爱上了枚举,但后来我开始评估它们的性能,我发现了一些让我感到难以置信的东西。下面是一个例子:
use std::time::{Instant};
pub enum MyEnum<'a> {
V1,
V2(&'a MyEnum<'a>),
V3,
}
impl MyEnum<'_> {
pub fn eval(&self) -> i64 {
match self {
MyEnum::V1 => 1,
MyEnum::V2(_) => 2,
MyEnum::V3 => 3,
}
}
pub fn eval2(&self) -> i64 {
match self {
MyEnum::V1 => 1,
MyEnum::V2(a) => a.eval2(),
MyEnum::V3 => 3,
}
}
}
fn main() {
const EXAMPLES: usize = 10000000;
let en = MyEnum::V1{};
let start = Instant::now();
let mut sum = 0;
for _ …Run Code Online (Sandbox Code Playgroud) 我移植了一个 Java 类,该类将所有可能的从 n 个选项中选择 k 个元素的无序组合循环到 Rust,希望 Rust 帮助我加快计算速度。但是当两者并肩运行时,结果发现 Java 快了几乎两倍!
由于这对我来说根本不合适,而且我刚刚开始使用 Rust,我一定是做错了什么,想知道是否有更多 Rust 经验的人能帮助我弄清楚为什么我的 Rust 代码慢得多。
这是我的通用接口、实现和测试代码的 Java 代码:
public interface Choice<Type> {
/**
* Returns the number of possible options that this choice provides.
*
* @return the number of choices
*/
public long getChoices();
/**
* Returns the choice with the given index.
*
* @param index - the index of the choice to return
* @return the choice of the given index
*/
public Type …Run Code Online (Sandbox Code Playgroud) 我正在初始化一个数组,然后将其反转多次以查看性能。
我想了解我是否编写了无法比较的代码,还是Rust真的很糟糕以至于花费了很多时间?
这是Rust的构建和计时过程:
rustc main.rs
time ./main
Run Code Online (Sandbox Code Playgroud)
这将继续下去。令人惊讶
fn reverse(mylist: &mut Vec<u16>) {
let length = mylist.len();
let mid_length = length / 2;
for number in 0..mid_length {
let mut a = mylist[number];
let mut b = mylist[length - number - 1];
mylist[number] = b;
mylist[length - number - 1] = a;
}
}
fn main() {
let array_size = 100000;
let iterations = 100000;
let mut v = vec![0u16; array_size];
for _ in 0..iterations {
reverse(&mut v);
} …Run Code Online (Sandbox Code Playgroud)