#timer #logging #durations #console #timing #javascript #time

timelog

A simple timer for logging time durations like console.time in JavaScript

6 stable releases

1.0.5 Oct 28, 2024
1.0.2 Oct 19, 2024
1.0.0 Oct 18, 2024

#760 in Web programming

40 downloads per month

MIT license

11KB
148 lines

timelog

A simple timer library for logging time durations, similar to console.time in JavaScript.

Features

  • Create multiple named timers
  • Start, log, and end timers
  • Print timing results in milliseconds
  • Support for optional additional messages

Installation

Add the following line to your Cargo.toml file:

[dependencies]
timelog = "1.0.0"

Usage

use timelog::Timer;

fn main() {
    let mut timer = Timer::new();

    timer.time("fetch_data");

    let duration: f64 = timer.time_log("fetch_data", true);
    println!("fetch_data took {:.2}ms", duration);
}

Documentation

timelog


lib.rs:

Timer

Timer is a Rust library for timing and logging time durations.

Features

  • Create multiple named timers
  • Start and stop timers
  • Log elapsed time without stopping the timer
  • Silent mode for logging without printing
  • Convert durations to milliseconds
  • End timers and get elapsed time
  • Singleton instance for global timing

Usage

Create a new Timer instance, start timers with labels, and log or stop them as needed. The library provides a simple and efficient way to measure execution time in your Rust programs.

Example

let mut timer = Timer::new();
timer.time("operation");
// Perform some operation
let elapsed = timer.time_log("operation", false);
println!("Operation took {} ms", elapsed);

// End a timer
let final_time = timer.time_end("operation");
println!("Final time: {} ms", final_time);

// Use singleton instance
Timer::single_instance().time("global_operation");
// Perform global operation
Timer::single_instance().time_end("global_operation");

This library is useful for performance monitoring and optimization in Rust applications. The time_end method allows you to stop a timer and get its final elapsed time. The single_instance feature provides a global Timer instance for convenient timing across your application.

Dependencies

~7.5–10MB
~182K SLoC