当前位置:网站首页>Rust: performance test criteria Library
Rust: performance test criteria Library
2020-11-08 11:21:00 【osc_ju8o7gji】
In the field of quantification , High performance requirements , Especially high frequency trading , That's nanosecond . stay RUST in , Test a function , Or a time-consuming operation, that is, performance analysis , How to do it ?
One 、 timer
Whether it can be used std::time::systime To calculate the time spent ? Let's have a try :
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);
}
You'll find that :
Why does it take to create a structure 3500 nanosecond , It's amazing . Actually , The real situation is not like this , It's because of the test error of the timer .
therefore , If you want to do a professional performance test , Or with the help of professional tripartite performance test library .
Two 、 Professional tripartite Library
RUST in , There are still many professional tripartite libraries , To push the library criterion.rs.
https://github.com/bheisler/criterion.rs
You can take a look at .
3、 ... and 、 example
1、 Create a test directory
In the engineering , Create a special test directory : I'm here in
my_bench Under the table of contents , Specifically created benches Catalog
2、toml file
about my_bench Under the project toml file
toml In file , increase :
[dev-dependencies]
criterion = "0.3"
[[bench]]
name = "my_benchmark"
harness = false
among ,name ="my_benchmark" It means something , It's not random . Here means , I am here my_bench Under the project , There is one my_benchmark.rs file , It has the performance test code I wrote . Don't go anywhere else .
3、 Prepare performance test code
because toml In file , Yes name =“my_benchmark” , that , The performance test code is my_benchmark.rs. Create this file .
Write the corresponding performance test code :
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、 function
In all the engineering my_bench Next , function cargo bench, That is, you can run the performance test code .
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%)
From above , We can get detailed test information , Including the time distribution of the operation .
Professional performance testing tools show that , Create a Stock object , It takes about the average 73ns, Not the timer 3500ns;
however , Create a Stock Of Box The object takes twice as long .
版权声明
本文为[osc_ju8o7gji]所创,转载请带上原文链接,感谢
边栏推荐
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- 第二次作业
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
- 漫画|讲解一下如何写简历&项目
- 如何将 PyTorch Lightning 模型部署到生产中
- Deeplight Technology Bluetooth protocol SRRC certification services
- 年轻一代 winner 的程序人生,改变世界的起点藏在身边
- 我们采访了阿里云云数据库SQL Server的产品经理,他说了解这四个问题就可以了...
- Installing MacOS 11 Big Sur in virtual machine
猜你喜欢

Rust:命令行参数与环境变量操作

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?

The container with the most water

Introduction to mongodb foundation of distributed document storage database

用科技赋能教育创新与重构 华为将教育信息化落到实处

值得一看!EMR弹性低成本离线大数据分析最佳实践(附网盘链接)

Game optimization performance (11) - Zhihu

仅用六种字符来完成Hello World,你能做到吗?

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

AMD Zen3首发评测:频率超5GHz,IPC提升不止19%,这次真的Yes了 - 知乎
随机推荐
Ali! Visual computing developer's series of manuals (with internet disk link)
2020-11-05
When kubernetes encounters confidential computing, see how Alibaba protects the data in the container! (Internet disk link attached)
The container with the most water
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
分布式文档存储数据库之MongoDB基础入门
运维人员常用到的 11 款服务器监控工具
2 days, using 4 hours after work to develop a test tool
临近双11,恶补了两个月成功拿下大厂offer,跳槽到阿里巴巴
你搞不懂与别人的差距,永远成不了架构师!月薪15K和月薪65K,你差在那了?
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
第二次作业
Bccoin tells you: what is the most reliable investment project at the end of the year!
What can your cloud server do? What is the purpose of cloud server?
The young generation of winner's programming life, the starting point of changing the world is hidden around
阿里出品!视觉计算开发者系列手册(附网盘链接)
Solve Safari browser download file name garbled problem
Mozi college SQL injection solution
仅用六种字符来完成Hello World,你能做到吗?