Table of Contents

TimeMeasure Class

Definition

Namespace
Cuemon.Diagnostics
Assemblies
Cuemon.Diagnostics.dll
Source
src/Cuemon.Diagnostics/TimeMeasure.cs

Provides a flexible, generic and lambda friendly way to perform time measuring operations.

public static class TimeMeasure
Inheritance
object
TimeMeasure

Examples

The following example demonstrates how to profile actions and functions using . It profiles parameterless and parameterized actions via WithAction, profiles functions with return values via WithFunc, and configures a completion threshold. Each profiler's elapsed time and, where applicable, return value are written to the console, illustrating how to measure execution duration of synchronous delegates.

using System;
using System.Threading;
using Cuemon.Diagnostics;

namespace MyApp.Examples;

public static class TimeMeasureExample
{
    public static void Demonstrate()
    {
        // Profile an action with no parameters
        TimeMeasureProfiler actionProfiler = TimeMeasure.WithAction(() =>
        {
            Thread.Sleep(50);
        });
        Console.WriteLine($"Action elapsed: {actionProfiler.Elapsed}");
        Console.WriteLine($"Member: {actionProfiler.Member}");

        // Profile an action with one parameter
        TimeMeasureProfiler paramProfiler = TimeMeasure.WithAction(
            (int ms) => Thread.Sleep(ms), 75);
        Console.WriteLine($"Param action elapsed: {paramProfiler.Elapsed}");

        // Profile a function that returns a value
        TimeMeasureProfiler<int> funcProfiler = TimeMeasure.WithFunc(() =>
        {
            Thread.Sleep(100);
            return 42;
        });
        Console.WriteLine($"Func result: {funcProfiler.Result}");
        Console.WriteLine($"Func elapsed: {funcProfiler.Elapsed}");

        // Profile a function with parameters
        TimeMeasureProfiler<string> greetProfiler = TimeMeasure.WithFunc(
            (string name) => $"Hello, {name}!", "World");
        Console.WriteLine($"Func result: {greetProfiler.Result}");

        // Configure options
        TimeMeasureProfiler configured = TimeMeasure.WithAction(
            () => Thread.Sleep(200),
            options => options.TimeMeasureCompletedThreshold = TimeSpan.FromMilliseconds(50));
        Console.WriteLine($"Configured elapsed: {configured.Elapsed}");
    }
}

Properties

Name Description
CompletedCallback

Gets or sets the callback that is invoked when a time measuring operation is completed.

Methods

Name Description
WithAction(Action, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync(Func<CancellationToken, Task>, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T>(Func<T, CancellationToken, Task>, T, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, CancellationToken, Task>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2>(Func<T1, T2, CancellationToken, Task>, T1, T2, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2, T3>(Func<T1, T2, T3, CancellationToken, Task>, T1, T2, T3, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2, T3, T4>(Func<T1, T2, T3, T4, CancellationToken, Task>, T1, T2, T3, T4, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2, T3, T4, T5>(Func<T1, T2, T3, T4, T5, CancellationToken, Task>, T1, T2, T3, T4, T5, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2, T3, T4, T5, T6>(Func<T1, T2, T3, T4, T5, T6, CancellationToken, Task>, T1, T2, T3, T4, T5, T6, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2, T3, T4, T5, T6, T7>(Func<T1, T2, T3, T4, T5, T6, T7, CancellationToken, Task>, T1, T2, T3, T4, T5, T6, T7, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2, T3, T4, T5, T6, T7, T8>(Func<T1, T2, T3, T4, T5, T6, T7, T8, CancellationToken, Task>, T1, T2, T3, T4, T5, T6, T7, T8, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithActionAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, CancellationToken, Task>, T1, T2, T3, T4, T5, T6, T7, T8, T9, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T>(Action<T>, T, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2>(Action<T1, T2>, T1, T2, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2, T3>(Action<T1, T2, T3>, T1, T2, T3, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2, T3, T4>(Action<T1, T2, T3, T4>, T1, T2, T3, T4, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2, T3, T4, T5>(Action<T1, T2, T3, T4, T5>, T1, T2, T3, T4, T5, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2, T3, T4, T5, T6>(Action<T1, T2, T3, T4, T5, T6>, T1, T2, T3, T4, T5, T6, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2, T3, T4, T5, T6, T7>(Action<T1, T2, T3, T4, T5, T6, T7>, T1, T2, T3, T4, T5, T6, T7, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2, T3, T4, T5, T6, T7, T8>(Action<T1, T2, T3, T4, T5, T6, T7, T8>, T1, T2, T3, T4, T5, T6, T7, T8, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithAction<T1, T2, T3, T4, T5, T6, T7, T8, T9>(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9>, T1, T2, T3, T4, T5, T6, T7, T8, T9, Action<TimeMeasureOptions>)

Profile and time measure the specified action delegate.

WithFuncAsync<TResult>(Func<CancellationToken, Task<TResult>>, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T, TResult>(Func<T, CancellationToken, Task<TResult>>, T, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, TResult>(Func<T1, T2, CancellationToken, Task<TResult>>, T1, T2, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, T3, TResult>(Func<T1, T2, T3, CancellationToken, Task<TResult>>, T1, T2, T3, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, CancellationToken, Task<TResult>>, T1, T2, T3, T4, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, T3, T4, T5, TResult>(Func<T1, T2, T3, T4, T5, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, T3, T4, T5, T6, TResult>(Func<T1, T2, T3, T4, T5, T6, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, T3, T4, T5, T6, T7, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFuncAsync<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, CancellationToken, Task<TResult>>, T1, T2, T3, T4, T5, T6, T7, T8, Action<AsyncTimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<TResult>(Func<TResult>, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T, TResult>(Func<T, TResult>, T, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, TResult>(Func<T1, T2, TResult>, T1, T2, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult>, T1, T2, T3, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, T3, T4, T5, TResult>(Func<T1, T2, T3, T4, T5, TResult>, T1, T2, T3, T4, T5, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, T3, T4, T5, T6, TResult>(Func<T1, T2, T3, T4, T5, T6, TResult>, T1, T2, T3, T4, T5, T6, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, T3, T4, T5, T6, T7, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, TResult>, T1, T2, T3, T4, T5, T6, T7, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.

WithFunc<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>, T1, T2, T3, T4, T5, T6, T7, T8, Action<TimeMeasureOptions>)

Profile and time measure the specified function delegate.