为什么这个 Rust 比我的类似 Python 慢?
Why is this Rust slower than my similar Python?
我有以下 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]);
}
根据 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()
当我 运行 编译的 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 user 0.02s system 99% cpu 1.120 total
运行 Python 给出:
rts@testbed $ time ./test.py
A declaration of independence by Kosovo will likely bring a similar declaration from Georgia's breakaway Abkhazia region, which Russia could well recognize.
./test.py 0.05s user 0.03s system 90% cpu 0.092 total
我知道println!
是一个扩展到更复杂的宏
::std::io::stdio::println_args(::std::fmt::Arguments::new({
#[inline]
#[allow(dead_code)]
static __STATIC_FMTSTR: &'static [&'static str] = &[""];
__STATIC_FMTSTR
},
&match (&lines[500],) {
(__arg0,) => [::std::fmt::argument(::std::fmt::String::fmt, __arg0)],
}));
不过,这看起来不像是会导致超过一秒的额外执行时间的事情。这些代码片段实际上不相似吗?我是否误解了将行读入向量并输出其中之一的最有效方法?
供参考 nc.txt
具有以下属性:
rts@testbed $ du -hs nc.txt
7.5M nc.txt
rts@testbed $ wc -l nc.txt
60219 nc.txt
使用优化标志构建它。
cargo run --release
我有以下 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]);
}
根据 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()
当我 运行 编译的 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 user 0.02s system 99% cpu 1.120 total
运行 Python 给出:
rts@testbed $ time ./test.py
A declaration of independence by Kosovo will likely bring a similar declaration from Georgia's breakaway Abkhazia region, which Russia could well recognize.
./test.py 0.05s user 0.03s system 90% cpu 0.092 total
我知道println!
是一个扩展到更复杂的宏
::std::io::stdio::println_args(::std::fmt::Arguments::new({
#[inline]
#[allow(dead_code)]
static __STATIC_FMTSTR: &'static [&'static str] = &[""];
__STATIC_FMTSTR
},
&match (&lines[500],) {
(__arg0,) => [::std::fmt::argument(::std::fmt::String::fmt, __arg0)],
}));
不过,这看起来不像是会导致超过一秒的额外执行时间的事情。这些代码片段实际上不相似吗?我是否误解了将行读入向量并输出其中之一的最有效方法?
供参考 nc.txt
具有以下属性:
rts@testbed $ du -hs nc.txt
7.5M nc.txt
rts@testbed $ wc -l nc.txt
60219 nc.txt
使用优化标志构建它。
cargo run --release