各语言测试:
time node index.js 1.37s user 0.19s system 121% cpu 1.282 total
time go build && ./main 1.59s user 0.02s system 80% cpu 2.002 total
time python3.9 main.py 2.45s user 0.27s system 98% cpu 2.762 total
time cargo run --release 2.46s user 0.42s system 97% cpu 2.972 total
time dart compile exe main.dart && bin/main 4.62s user 0.45s system 114% cpu 4.415 total
rust code:
use std::fs::File;
use std::io::{Read};
use serde_json::Value;
fn parse_json(contents: Vec) {
let now = std::time::Instant::now();
let _: Value = serde_json::from_slice(&contents).unwrap();
let elapsed = now.elapsed();
println!("elapsed: {:?}", elapsed);
}
fn main() {
let contents = {
let mut vec = Vec::new();
// https://github.com/json-iterator/test-data/blob/master/large-file.json
File::open("large-file.json").unwrap().read_to_end(&mut vec).unwrap();
vec
};
for _ in 0..10 {
parse_json(contents.clone());
}
}
测试机器:MacBook Pro (16-inch, 2019) 2.3 GHz 八核 Intel Core i9 32 GB 2667 MHz DDR4
不好意思,上面使用time统计时间是不包含程序编译时间的,只是发主题的时候合到一起的。
for 循环打印的结果如下:
NodeJs: 110 ms
NodeJs: 107 ms
NodeJs: 103 ms
NodeJs: 106 ms
NodeJs: 128 ms
NodeJs: 94 ms
NodeJs: 93 ms
NodeJs: 92 ms
NodeJs: 98 ms
NodeJs: 93 ms
Dart: 384 ms
Dart: 390 ms
Dart: 377 ms
Dart: 395 ms
Dart: 356 ms
Dart: 381 ms
Dart: 378 ms
Dart: 370 ms
Dart: 388 ms
耗时: 379 ms
Python: 241.057 ms
Python: 233.375 ms
Python: 249.352 ms
Python: 244.968 ms
Python: 249.487 ms
Python: 288.286 ms
Python: 263.347 ms
Python: 255.939 ms
Python: 256.224 ms
Python: 252.851 ms
Golang: 171 ms
Golang: 162 ms
Golang: 160 ms
Golang: 161 ms
Golang: 161 ms
Golang: 160 ms
Golang: 165 ms
Golang: 157 ms
Golang: 161 ms
Golang: 158 ms
Rust: 297.331601ms
Rust: 268.03852ms
Rust: 280.606788ms
Rust: 272.826854ms
Rust: 293.53427ms
Rust: 269.841906ms
Rust: 285.450976ms
Rust: 288.862249ms
Rust: 275.984671ms
Rust: 276.715469ms