timetrap 0.1.3

Macros to wrap your code and measure time
Documentation

timetrap

GitHub CI Crates.io docs.rs

Lightweight Rust macros for timing and memory/swap measurement. All macros return the wrapped expression result as-is (R), so they can be used inline without changing function behavior.

Install

cargo add timetrap

trap! (time only)

use timetrap::*;

let count = trap!("parse_config", {
    let config = "a=1,b=2";
    config.split(',').count()
});

assert_eq!(2, count);

trap_mem! (time + memory/swap)

use timetrap::*;

let map = trap_mem!("build_map", MemUnits::Mb, {
    let mut map = std::collections::HashMap::with_capacity(100_000);
    for i in 0..100_000u64 {
        map.insert(i, i);
    }
    map
});

assert_eq!(100_000, map.len());

Colored Output

use timetrap::*;

trap!("task", color = Colors::Green, {
    1 + 1
});

trap_mem!("task_mem", MemUnits::Kb, color = Colors::Cyan, {
    vec![0_u8; 4096]
});

Color is printed only when stdout is a TTY and NO_COLOR is not set.