Attribute Macro fun_time::fun_time

source ·
#[fun_time]
Expand description

Measure the execution times of the function under the attribute.

It does this by wrapping the function in a new block surrounded by a [std::time::Instant::now()] call and a [std::time::Instant::elapsed()] call. It then either logs the duration directly or returns it with the original functions return value, depending on how you configured this attribute.

Attributes

when

The when attribute can be used to configure when the timing information is collected. For example, with "always" the timing information is always collected, but with "debug" the timing information is only collected if the cfg!(debug_assertions) statement evaluates to true.

give_back

The give_back attribute can be used to switch the macro from printing mode to returning the captured elapsed time together with the original return value of the function. It will modify the original return value to be a tuple, where the first value is the original return value and the second value is the elapsed time as a [std::time::Duration] struct.

message

The message attribute allows you to set a message that will be displayed in the case you chose to let the macro report the elapsed time directly. This message will be shown both in the start and done messages.

reporting

The reporting attribute determines how the message and elapsed time will be displayed directly when you have chosen not to let the macro return the elapsed time to you. By default it uses a simple println! statement, but with the optional log feature it will use the log crate to log it using the info! macro.

Example

// Replace this in your code with `use fun_time::fun_time;`
use fun_time_derive::fun_time;

#[fun_time(give_back)]
fn function_with_heavy_calculations(some_data: Vec<i32>) -> bool {
    // Big brain calculations...
    true
}

fn main() {
    let my_data = vec![1, 2, 3];

    // Run the function and receive timing information
    let (my_original_return_value, how_long_did_it_take) = function_with_heavy_calculations(my_data);
}