tea-timer

Crates.iotea-timer
lib.rstea-timer
version0.1.2
sourcesrc
created_at2024-09-05 16:45:18.188395
updated_at2024-09-09 02:30:24.822616
descriptionA simple and efficient Rust library for measuring and reporting the duration of tasks
homepage
repositoryhttps://github.com/Teamon9161/tea-timer
max_upload_size
id1364854
size12,627
Teamon (Teamon9161)

documentation

README

Tea Timer

Tea Timer is a simple and efficient Rust library for measuring and reporting the duration of tasks. It provides an easy-to-use API for creating timers, measuring elapsed time, and formatting durations.

Features

  • Create named timers
  • Measure elapsed time
  • Format durations in a human-readable format
  • Restart timers with new task names
  • Optional logging support using the log crate

Installation

Add this to your Cargo.toml:

tea-timer = "0.1.0"

Usage

Macro Usage

let result = tea_timer::took! {
    // ...any code
};
// this will print elapsed time and get result of thecode block

Function Usage

use tea_timer::took;

let result = took(|| {
    // ...any code
}, "task");
// this will print elapsed time and get result of the function

Basic Usage

use tea_timer::Timer;
use std::thread::sleep;
use std::time::Duration;

let mut timer = Timer::new("task");
// Simulate some work with a sleep
sleep(Duration::from_secs(2));
// this will print elapsed time
timer.elapsed();
// Restart the timer with a new task name
timer.restart("new_task");
// Simulate more work
sleep(Duration::from_millis(500));
// Measure elapsed time again
// consume timer and print elapsed time
timer.stop();

Logging Usage

use tea_timer::Timer;
use std::thread::sleep;
use std::time::Duration;

let mut timer = Timer::new("task");
timer.log();  // This will log the elapsed time using the log crate
Commit count: 0

cargo fmt