相关疑难解决方法(0)

为什么read_line(..)比lines()慢得多?

下面的代码在调用时运行速度要慢read_line(..)得多lines() 你不能在操场上运行它但是对我来说这打印了以下代码

lines()     took Duration { secs: 0, nanos: 41660031 }
read_line() took Duration { secs: 2, nanos: 379397138 }
Run Code Online (Sandbox Code Playgroud)

实施Lines确实差不多就是我写的(但更多的!)为什么会出现这样的差异?

use std::net::{TcpListener, TcpStream};
use std::io::{BufRead, BufReader, Write};
use std::thread;

fn main() {

    let listener = TcpListener::bind("127.0.0.1:80")
        .expect("listen failed");
    thread::spawn(move || {
        for stream in listener.incoming() {
            let mut stream = stream.unwrap();
            thread::spawn(move || {
                for x in 1..1000 + 1 {
                    stream.write_all(format!("{}\n", x).as_bytes())
                        .expect("write failed");
                }
            });
        }
    });

    let start_a = std::time::Instant::now(); …
Run Code Online (Sandbox Code Playgroud)

io rust

1
推荐指数
1
解决办法
383
查看次数

标签 统计

io ×1

rust ×1