File: performance.rs

package info (click to toggle)
rust-flexi-logger 0.29.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,084 kB
  • sloc: makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::fmt;
use std::time::Instant;

struct Struct {
    data: [u8; 32],
}

impl fmt::Display for Struct {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.data)
    }
}

fn main() {
    // --------------------------------
    println!("flexi_logger");
    flexi_logger::Logger::try_with_str("off")
        .unwrap()
        .format(flexi_logger::detailed_format)
        .start()
        .unwrap();
    // --------------------------------
    // $> Set-Item -Path Env:RUST_LOG -Value "trace"
    // println!("env_logger");
    // env_logger::init();
    // $> Set-Item -Path Env:RUST_LOG
    // --------------------------------
    let mut structs = Vec::new();
    for i in 0..100 {
        structs.push(Struct {
            data: [i as u8; 32],
        });
    }

    {
        // With format
        let start = Instant::now();
        for s in &structs {
            log::info!("{}", format!("{s}"));
        }
        eprintln!("with format: {:?}", start.elapsed()); // 2-7ms
    }

    {
        // Plain logger
        let start = Instant::now();
        for s in &structs {
            log::info!("{}", s);
        }
        eprintln!("plain: {:?}", start.elapsed()); // 17-26ms
    }
}