当前位置:网站首页>Rust : 性能测试criterion库
Rust : 性能测试criterion库
2020-11-08 11:21:00 【osc_ju8o7gji】
在量化领域,对性能要求比较高,特别是高频交易,那是纳秒必争。在RUST中,测试一个函数,或一个操作耗时即性能分析,应是如何做呢?
一、计时器
是否可以用std::time::systime 来计算花时情况?我们来试一试:
use std::time::SystemTime;
pub struct Stock{
pub price:f64,
pub volume:i32,
pub code:String,
pub amount:f64,
}
impl Stock{
fn default()->Self{
let p =10000.0_f64;
let v =1_i32;
Stock{
price:p,
volume:v,
code:String::from("SH600036"),
amount: p*v as f64,
}
}
}
fn main() {
let now = SystemTime::now();
let stock = Stock::default();
let elapsed = now.elapsed().unwrap().as_nanos();
println!("cost time :{:?} ",elapsed);
}
你会发现:
为什么创建一个结构体要花3500纳秒,这也太不可思议了吧。其实,真实的情况并不是这样的,是因为计时器的测试误差导致。
所以,如果要进行专业的性能测试,还是要借助于专业三方性能测试库。
二、专业三方库
RUST中,专业三方库还是有不少,推库用criterion.rs.
https://github.com/bheisler/criterion.rs
具体大家可以看看。
三、实例
1、创建测试目录
在工程中,建一个专门的测试用的目录:我这儿是在
my_bench目录下,专门创建了benches目录
2、toml文件
对于my_bench工程下toml文件
toml文件中,增加:
[dev-dependencies]
criterion = "0.3"
[[bench]]
name = "my_benchmark"
harness = false
其中,name ="my_benchmark"是有所指的,不是随便写的。这里是指,我在my_bench工程下,有一个my_benchmark.rs文件,里面有我写的性能测试代码。别的地方就不要去找了。
3、准备性能测试代码
因为toml文件中,有name =“my_benchmark” ,那么,性能测试代码就是my_benchmark.rs. 创建这个文件。
写入相应的性能测试代码:
use criterion::{
black_box, criterion_group, criterion_main, Criterion};
pub struct Stock{
pub price:f64,
pub volume:i32,
pub code:String,
pub amount:f64,
}
impl Stock{
fn default()->Self{
let p =10000.0_f64;
let v =1_i32;
Stock{
price:p,
volume:v,
code:String::from("SH600036"),
amount: p*v as f64,
}
}
}
pub fn set_heap()->Stock{
Stock::default()
}
pub fn box_stock() ->Box<Stock>{
Box::new(Stock::default())
}
fn criterion_benchmark_heap(c: &mut Criterion) {
c.bench_function("stock ", |b| b.iter(|| set_heap()));
}
fn criterion_benchmark_box(c: &mut Criterion) {
c.bench_function("box<stock> ", |b| b.iter(|| box_stock()));
}
criterion_group!(benches, criterion_benchmark_heap,criterion_benchmark_box);
criterion_main!(benches);
4、运行
在所有的工程my_bench下,运行cargo bench,即可以进行性能测试代码的运行了。
Finished bench [optimized] target(s) in 2.80s
Running target\release\deps\my_bench-95230ab505784caf.exe
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Running target\release\deps\my_benchmark-a468db13a6ba0ea3.exe
Gnuplot not found, using plotters backend
stock time: [75.387 ns 83.206 ns 91.985 ns]
Found 3 outliers among 100 measurements (3.00%)
3 (3.00%) high mild
box<stock> time: [168.68 ns 189.43 ns 212.00 ns]
Found 3 outliers among 100 measurements (3.00%)
从上面,我们可以得到详细的测试信息,包括运行的时间分布情况。
专业性能测试工具表明,创建一个Stock对象,大约平均需要73ns,而不是计时器显示的3500ns;
但是,创建一个Stock 的Box对象需要的时间是双倍。
版权声明
本文为[osc_ju8o7gji]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4288530/blog/4708045
边栏推荐
- 虚拟机中安装 macOS 11 big sur
- 一个方案提升Flutter内存利用率
- 如何将 PyTorch Lightning 模型部署到生产中
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
- Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba
- It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
- Analysis of istio access control
- AMD Zen3首发评测:频率超5GHz,IPC提升不止19%,这次真的Yes了 - 知乎
- 渤海银行百万级罚单不断:李伏安却称治理完善,增速呈下滑趋势
猜你喜欢

狗狗也能操作无人机!你没看错,不过这其实是架自动驾驶无人机 - 知乎

入门级!教你小程序开发不求人(附网盘链接)

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

软件测试培训班出来好找工作么
![[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter](/img/3b/00bc81122d330c9d59909994e61027.jpg)
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter

If you don't understand the gap with others, you will never become an architect! What's the difference between a monthly salary of 15K and a monthly salary of 65K?

It's 20% faster than python. Are you excited?

阿里教你深入浅出玩转物联网平台!(附网盘链接)

Close to the double 11, he made up for two months and successfully took the offer from a large factory and transferred to Alibaba

推荐一部经济科普视频,很有价值!
随机推荐
Which is more worth starting with the difference between vivos7e and vivos7
软件测试就是这么回事?!
一个方案提升Flutter内存利用率
狗狗也能操作无人机!你没看错,不过这其实是架自动驾驶无人机 - 知乎
最全!阿里巴巴经济体云原生实践!(附网盘链接)
Win10 Terminal + WSL 2 安装配置指南,精致开发体验
Is there a big difference between i5 1135g7 and i51035g1? Which is better?
2020-11-05
Can you do it with only six characters?
Japan PSE certification
The most complete! Alibaba economy cloud original practice! (Internet disk link attached)
A scheme to improve the memory utilization of flutter
游戏优化性能杂谈(十一) - 知乎
抖音直播监控Api:随机推荐
The container with the most water
Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
虚拟机中安装 macOS 11 big sur
[data structure Python description] use hash table to manually implement a dictionary class based on Python interpreter