From 79efa59a34adb0228c742c6ca9386c9c3a7be772 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 7 Nov 2020 12:23:25 +0800 Subject: [PATCH] feat: add __performance --- README.md | 4 +++- __performance/print_perf/Cargo.lock | 14 ++++++++++++ __performance/print_perf/Cargo.toml | 10 ++++++++ __performance/print_perf/src/main.rs | 34 ++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 __performance/print_perf/Cargo.lock create mode 100644 __performance/print_perf/Cargo.toml create mode 100644 __performance/print_perf/src/main.rs diff --git a/README.md b/README.md index 9ab929f..8730913 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ Project or files: │   ├── pcap │   ├── tcp │   └── udp_laminar +├── __performance +│   └── print_perf ├── __search │   ├── simsearch │   └── tantivy @@ -125,7 +127,7 @@ Project or files: ├── vec.rs └── while.rs -97 directories, 26 files +99 directories, 26 files ``` diff --git a/__performance/print_perf/Cargo.lock b/__performance/print_perf/Cargo.lock new file mode 100644 index 0000000..a79aa93 --- /dev/null +++ b/__performance/print_perf/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "print_perf" +version = "0.1.0" +dependencies = [ + "print_perf 0.1.9", +] + +[[package]] +name = "print_perf" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04378d9c598036cd5c61d71245b4e5e3e6455d1f2ab2120a7b9055dace8d8a47" diff --git a/__performance/print_perf/Cargo.toml b/__performance/print_perf/Cargo.toml new file mode 100644 index 0000000..02fd117 --- /dev/null +++ b/__performance/print_perf/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "print_perf" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +print_perf = "0.1" diff --git a/__performance/print_perf/src/main.rs b/__performance/print_perf/src/main.rs new file mode 100644 index 0000000..fc2c393 --- /dev/null +++ b/__performance/print_perf/src/main.rs @@ -0,0 +1,34 @@ +use print_perf::*; +// or explicit print_perf::{perf, Perf}; +use std::time::Duration; +use std::thread::sleep; + +fn add(a: i32, b: i32) -> i32 { + sleep(Duration::from_millis(100)); + a + b +} + +fn main() { + println!("main_1()"); + main_1(); + println!("main_2()"); + main_2(); +} + +fn main_1() { + let add_p = perf!("add fn"); + let result = add(4, 4); + add_p.end(); + // ^-- prints: 0.100140446 (add fn) @ [src/main.rs:9] + assert_eq!(result, 8); +} + + +fn main_2() { + let p = perf!("add fn"); + let _result = add(4, 4); + p.split("add"); + let _div = _result / 2; + p.split("div"); + p.end(); +}