sy/stopwatch

Utility class for timing

1.0.0 2020-10-02 10:54 UTC

This package is auto-updated.

Last update: 2024-10-11 01:06:59 UTC


README

Utility class for timing

Installation

Install the latest version with

$ composer require sy/stopwatch

Basic Usage

<?php

use Sy\Stopwatch;

$stopwatch = new Stopwatch();

// Start timing
$stopwatch->start();

// Process to be timed
for ($i = 0; $i < 100000; $i++) {
	// Do something...
}

// Stop timing
$stopwatch->stop();

// Retrieve time in seconds
$time = $stopwatch->getTime();
echo "Execution time: $time seconds";

Advanced Usage

You can add a specific label for each timer.

After a stop, you start the stopwatch again, the time will be added to timer. Otherwise you have to reset the stopwatch.

Example:

<?php

use Sy\Stopwatch;

$stopwatch = new Stopwatch();

for ($i = 0; $i < 10; $i++) {
	
	// Start timing process A
	$stopwatch->start('A');
	
	// Process A
	for ($a = 0; $a < 1000; $a++) {
		// Do something...
	}

	// Stop timing process A
	$stopwatch->stop('A');

	// Start timing process B
	$stopwatch->start('B');

	for ($b = 0; $b < 1000; $b++) {
		// Do something...
	}

	// Stop timing process B
	$stopwatch->stop('B');
}

// Retrieve time
$timeA = $stopwatch->getTime('A');
$timeB = $stopwatch->getTime('B');