Crates.io | elapsed-printer |
lib.rs | elapsed-printer |
version | 0.1.0 |
source | src |
created_at | 2021-11-30 06:35:10.246798 |
updated_at | 2021-11-30 06:35:10.246798 |
description | Very simple macro for printing time elapsed to execute a function |
homepage | |
repository | https://github.com/baku4/elapsed-printer/ |
max_upload_size | |
id | 489672 |
size | 16,052 |
Very simple macro for printing time elapsed to execute a function.
elapsed-printer
is crate holding just one macro, print_elapsed
using Rust standard library std::time
to check elapsed during function(also method) execution.
print_elapsed
can have three types of attributes. Using attributes is optional and, if not specified, uses the default attributes. The order of attributes and the use of quotes do not matter.
stdout
- Print output to standard output stream.stderr
- Print output to standard error stream.both
- Print output to both standard output and error stream.Default
: stdout
auto
- Print output in the form defined in Debug
trait in std::time::Duration
structure.s
- Print output in units of second.ms
- Print output in units of millisecond.us
- Print output in units of microsecond.ns
- Print output in units of nanosecond.Default
: auto
Default
: not specified (=print always)use elapsed_printer::print_elapsed;
#[print_elapsed]
fn func_to_print_elapsed_default() {}
#[print_elapsed(stdout, auto)]
// Same as default
// Print always regardless of feature activation.
fn func_to_print_elapsed_same_as_default() {}
#[print_elapsed(ms, "stdout")]
// Attribute order does not matter.
// Use of quotes does not matter.
fn func_to_print_elapsed_same_with_ms() {}
#[print_elapsed("ms", stderr, [feature_1])]
// Print when using `feature_1`
fn func_to_print_elapsed_when_using_feature_1() {}
#[print_elapsed([feature_1, feature_2], ns, stderr)]
// Print when using `feature_1` or `feature_2`
fn func_to_print_elapsed_when_using_feature_1_or_feature_2() {}
struct MyStruct;
impl MyStruct {
#[print_elapsed]
// Can be applied to method
pub fn method(&self) {}
}
Code
use elapsed_printer::print_elapsed;
use std::time::Duration;
use std::thread;
#[print_elapsed]
fn function_name_1() {
thread::sleep(Duration::from_millis(10));
}
#[print_elapsed(stdout, ns)]
fn function_name_2() {
//
}
#[print_elapsed(stdout, us)]
fn function_name_3() {
function_name_1()
}
fn main() {
function_name_1();
function_name_2();
function_name_3();
function_name_1();
}
Output
function_name_1, 12.527014ms
function_name_2, 32ns
function_name_1, 10.070776ms
function_name_3, 10097us