#execution-time #timing #function #measure #measure-time #time

fun_time

fun_time is a simple Rust library that allows you to easily time your function calls with a simple attribute!

8 releases

0.3.4 Jan 18, 2024
0.3.3 Oct 14, 2023
0.3.0 Sep 21, 2023
0.2.1 Jan 2, 2023
0.1.0 Dec 23, 2022

#65 in Profiling

Download history 549/week @ 2024-08-14 466/week @ 2024-08-21 617/week @ 2024-08-28 457/week @ 2024-09-04 608/week @ 2024-09-11 575/week @ 2024-09-18 1489/week @ 2024-09-25 1592/week @ 2024-10-02 1730/week @ 2024-10-09 786/week @ 2024-10-16 618/week @ 2024-10-23 655/week @ 2024-10-30 656/week @ 2024-11-06 996/week @ 2024-11-13 1078/week @ 2024-11-20 720/week @ 2024-11-27

3,722 downloads per month
Used in 6 crates (4 directly)

MIT license

10KB
137 lines

fun_time

Crates.io docs.rs

fun_time is a simple Rust library that allows you to easily time your function calls with a simple attribute!

Basic example

#[fun_time(message = "Heavy calculations on: {a_value}")]
fn some_cool_function(a_value: String) -> usize {
    a_value.len()
}

fn main() {
    let my_value_length = some_cool_function(String::from("Hello, world."));
}

The above will print Starting: Heavy calculations on: Hello, world. when the function starts, and Heavy calculations on: Hello, world.: Done in <duration> on completion.

Configuration

There are various attributes that allow you to configure the behavior of the fun_time attribute.

  • message allows you to set a message that will be printed when starting, and when done, the message is passed directly to the format! macro, so the arguments to the function can be used in the message (provided they have Debug or Display).
  • when allows you to configure when the timing should be collected. The possible values for this are: "always" which as the name might suggest will always collect timing information, and "debug" which will only collect when cfg!(debug_assertions) evaluates to true.
  • give_back is a flag that makes it so the wrapped function will now return the elapsed time instead of printing it. For this it modifies the return type from for example: -> &'a str to -> (&'a str, std::time::Duration). This allows you to handle printing or storing the timing information.
  • reporting (can not be used in combination with give_back) determines how the reporting is done. The possible options are: "println" which will print to stdout using println!. The "log" option is only available when the log feature is used. This will use the log crate with info! level logs by default, this can be affected by the level option.
  • level Set the level for the log messages, can by any option that can be parsed by the log::Level enum.

Reporting

The reported messages are formatted as follows:

Start message: "Starting: YOUR_MESSAGE_HERE"

Done message: "YOUR_MESSAGE_HERE: Done in DURATION"

Dependencies

~2MB
~45K SLoC