Rust: tokio,异步代码与运行速度初探

几个问题:
1、同样的一段代码,放在异步中,运行速度会如何?
2、什么情况下异步并没有改变运行速度?
3、如何提升异步的速度?

toml:

[dependencies]
tokio = { version = "1", features = ["full"] }
futures = "0.3.4"

代码:

use std::time::{Duration, Instant};
use std::thread;
use tokio::time;
const N:i32 =1000000;
struct bar{
    price :f64,
    code: Option<String>,
    close: f64,
    open:f64,
    high:f64,
    low:f64,
}
impl bar{
    fn default()->Self{
        bar{
            price:0.0,
            code :Some(String::from("I AM NULL")),
            close:0.0,
            open:0.0,
            high:0.0,
            low:0.0,
        }
    }
    fn push_vec(n:i32)->Vec<Self>{
        let mut v = vec![];
        for _ in 0..N{
            v.push(bar::default());
        }
        v
    }
}



fn hello(){
    let v = bar::push_vec(N);
    println!("sync hello world ! v length :{:?}",v.len());
}

async fn async_hello(){
    let v = bar::push_vec(N);
    println!("async hello world ! v length :{:?}",v.len());
}
// 同步sleep
async fn sync_time_01() {
    std::thread::sleep(time::Duration::from_secs(1));//同步sleep
}
async fn sync_time_02() {
    std::thread::sleep(time::Duration::from_secs(1))//同步sleep
}
// 异步sleep
async fn async_time_01() {
    tokio::time::sleep(Duration::from_secs(1)).await;//异步sleep
}
async fn async_time_02() {
    tokio::time::sleep(Duration::from_secs(1)).await;//异步sleep
}
async fn sync_do() {
    async_hello().await;
    tokio::join!(sync_time_01(),sync_time_02());// 并行跑
    println!("sync_do is over!")
}

async fn async_do() {
    async_hello().await;
    tokio::join!(async_time_01(),async_time_02(),async_time_01(),async_time_02());//并行跑
    println!("async_do is over!")
}

#[tokio::main]
async fn main() {
    let start_0 = Instant::now();
    hello();
    println!("hello cost miliseconds[毫秒] : {}", start_0.elapsed().as_millis());

    let start_1 = Instant::now();
    async_hello().await;
    println!("async_hello cost miliseconds[毫秒] : {}", start_1.elapsed().as_millis());


    let start_2 = Instant::now();
    sync_do().await;
    println!("sync_do cost miliseconds[毫秒] : {}", start_2.elapsed().as_millis());
    let start_3 = Instant::now();
    async_do().await;
    println!("async_do cost miliseconds[毫秒] : {}", start_3.elapsed().as_millis());

}

ouput:


    Finished release [optimized] target(s) in 2.08s
     Running `target/release/test_trait`
sync hello world ! v length :1000000
hello cost miliseconds[毫秒] : 179
async hello world ! v length :1000000
async_hello cost miliseconds[毫秒] : 148
async hello world ! v length :1000000
sync_do is over!
sync_do cost miliseconds[毫秒] : 2137
async hello world ! v length :1000000
async_do is over!
async_do cost miliseconds[毫秒] : 1143

你能得出什么结论么?
(1)同一段代码(同步),放在异步中,花时间是一样的。尽管上面的数据好象异步更少,其实是一样的。
(2)异步要用并行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值