Class PerfTimer
A performance timer that measures the elapsed time for different actions and intervals.
Inherited Members
Namespace: mk.helpers
Assembly: mk.helpers.dll
Syntax
public class PerfTimer
Constructors
PerfTimer()
Initializes a new instance of the PerfTimer class with default settings (enabled).
Declaration
public PerfTimer()
PerfTimer(bool)
Initializes a new instance of the PerfTimer class with the specified enabled status.
Declaration
public PerfTimer(bool enabled)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | enabled | True to enable the timer, false to disable it. |
Properties
AllTimes
Gets a collection of all recorded action times.
Declaration
public ICollection<TimeSpan> AllTimes { get; }
Property Value
| Type | Description |
|---|---|
| ICollection<TimeSpan> |
AllTimesElapsed
Gets the sum of all recorded action times.
Declaration
public TimeSpan AllTimesElapsed { get; }
Property Value
| Type | Description |
|---|---|
| TimeSpan |
Elapsed
Gets the elapsed time for the current action (since Start() was called).
Declaration
public TimeSpan Elapsed { get; }
Property Value
| Type | Description |
|---|---|
| TimeSpan |
Methods
AddCheckpoint(string)
Add new checkpoint (store name, time). Timer is not reset!
Declaration
public PerfTimer AddCheckpoint(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | name of the checkpoint |
Returns
| Type | Description |
|---|---|
| PerfTimer | The PerfTimer instance for method chaining. |
AddCheckpoint(string, TimeSpan)
Add new checkpoint (store name, time). timer is not effected.
Declaration
public PerfTimer AddCheckpoint(string name, TimeSpan timeSpan)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | name of the checkpoint |
| TimeSpan | timeSpan | timespan to store |
Returns
| Type | Description |
|---|---|
| PerfTimer | The PerfTimer instance for method chaining. |
Clear()
Clears all recorded action times, checkpoints will remain the same.
Declaration
public void Clear()
ClearCheckpoints()
Clears all recorded checkpoints.
Declaration
public PerfTimer ClearCheckpoints()
Returns
| Type | Description |
|---|---|
| PerfTimer |
Execute(Action)
Executes the specified action and records the elapsed time if the timer is enabled.
Declaration
public PerfTimer Execute(Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | action | The action to be executed. |
Returns
| Type | Description |
|---|---|
| PerfTimer | The PerfTimer instance for method chaining. |
ExecuteWithCheckpoint(string, Action)
Executes the specified action and records the elapsed time in a checkpoint. the timer is not effected and continues to run / keep its current state.
Declaration
public PerfTimer ExecuteWithCheckpoint(string name, Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | name of the checkpoint |
| Action | action | The action to be executed. |
Returns
| Type | Description |
|---|---|
| PerfTimer | The PerfTimer instance for method chaining. |
GetCheckpoint(string)
Get the timespan of a saved checkpoint, default is TimeSpan.Zero
Declaration
public TimeSpan GetCheckpoint(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | name of the checkpoint |
Returns
| Type | Description |
|---|---|
| TimeSpan |
GetCheckpoints()
Gets a collection of all recorded checkpoints.
Declaration
public IDictionary<string, TimeSpan> GetCheckpoints()
Returns
| Type | Description |
|---|---|
| IDictionary<string, TimeSpan> |
Interval()
Stops the timer, records the elapsed time, and starts the timer again. Used to measure intervals between actions. This does not clear checkpoints.
Declaration
public PerfTimer Interval()
Returns
| Type | Description |
|---|---|
| PerfTimer | The PerfTimer instance for method chaining. |
RemoveCheckpoint(string)
Removes a checkpoint
Declaration
public PerfTimer RemoveCheckpoint(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | name of the checkpoint |
Returns
| Type | Description |
|---|---|
| PerfTimer |
Reset()
Resets the timer to measure the elapsed time for the next action.
Declaration
public PerfTimer Reset()
Returns
| Type | Description |
|---|---|
| PerfTimer | The PerfTimer instance for method chaining. |
Start()
Starts the timer to measure the elapsed time for an action.
Declaration
public PerfTimer Start()
Returns
| Type | Description |
|---|---|
| PerfTimer | The PerfTimer instance for method chaining. |
StartNew()
Create new instance of PerfTimer and start it
Declaration
public static PerfTimer StartNew()
Returns
| Type | Description |
|---|---|
| PerfTimer |
Stop()
Stops the timer and records the elapsed time for the completed action.
Declaration
public PerfTimer Stop()
Returns
| Type | Description |
|---|---|
| PerfTimer | The PerfTimer instance for method chaining. |