Code Monkey home page Code Monkey logo

stopwatch's Introduction

Simple stopwatch

Primitive stopwatch, with simple start and stop methods, for measurement of every single time periods and all time whole.

How to install

composer require sbwerewolf/stopwatch

How to use

echo 'Duration is ' .
    (new \SbWereWolf\Stopwatch\HRTimeStopwatch())
    ->start()->stop()->getLastTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;
Duration is 300 ns

Stopwatch available with different engines:

  • hrtime()
  • microtime()
  • DateTimeImmutable

\SbWereWolf\Stopwatch\HRTimeStopwatch implements hrtime() engine.

\SbWereWolf\Stopwatch\MicroTimeStopwatch implements microtime() engine.

\SbWereWolf\Stopwatch\DateTimeStopwatch implements DateTimeImmutable engine.

Advanced usage

$stopwatch = new SbWereWolf\Stopwatch\HRTimeStopwatch();
echo (new DateTimeImmutable())->format('s.u') . PHP_EOL;

$stopwatch->start();
time_nanosleep(0, 100);
$stopwatch->stop();

echo (new DateTimeImmutable())->format('s.u') .
    ' Period 1 duration: ' .
    $stopwatch->getLastTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;

time_nanosleep(0, 100);

$stopwatch->start();
time_nanosleep(0, 100);
$stopwatch->stop();

echo (new DateTimeImmutable())->format('s.u') .
    ' Period 2 duration: ' .
    $stopwatch->getLastTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;

/* Summa of all time periods */
echo (new DateTimeImmutable())->format('s.u') .
    ' Periods 1 + 2 summary duration: ' .
    $stopwatch->getSummaryTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;

/* Overall time */
echo (new DateTimeImmutable())->format('s.u') .
    ' Whole process duration: ' .
    $stopwatch->getWholeTime()->asNanoSeconds() .
    ' ns' .
    PHP_EOL;
02.380004
02.380020 Period 1 duration: 12400 ns
02.380032 Period 2 duration: 3600 ns
02.380035 Periods 1 + 2 summary duration: 16000 ns
02.380044 Whole process duration: 24000 ns

Using a stopwatch to benchmark any process

$stopwatch = new SbWereWolf\Stopwatch\HRTimeStopwatch();
$benchmark = new SbWereWolf\Stopwatch\Benchmark($stopwatch);

$delay = 100;
echo "Variable value before step z callback `$delay`" . PHP_EOL;
/* Variable value before step z callback `100` */
$benchmark->step('z', function () use ($delay) {
    time_nanosleep(0, $delay);
    $delay++;
});
echo "after step z callback `$delay`" . PHP_EOL;
/* after step z callback `100` */
/* Variable does not change its value */

$benchmark->step('x', function () use (&$delay) {
    time_nanosleep(0, $delay);
    $delay += 999;
});
echo "after step x callback `$delay`" . PHP_EOL;
/* after step x callback `1099` */
/* Variable does change its value */

$benchmark->step('c', function () use (&$delay) {
    $delay -= 999;
    time_nanosleep(0, $delay);
});
echo "after step c callback `$delay`" . PHP_EOL;
/* after step c callback `100` */
/* Variable does change its value */

echo "Benchmark steps measurement is:" . PHP_EOL;
$i=0;
foreach ($benchmark->report() as $desc => $val) {
    /** @var SbWereWolf\Stopwatch\ITimerReadings $val */
    echo "$desc => {$val->asNanoSeconds()} ns" . PHP_EOL;
    $i++;
}

$totalNanoseconds = $benchmark->total()->asNanoSeconds();
echo "Total is $totalNanoseconds ns";
variable value before step z `100`
after step z `100`
after step x `1099`
after step c `100`
Benchmark steps measurement is:
z => 15800 ns
x => 7600 ns
c => 4100 ns
Total is 27500 ns

Contacts

Volkhin Nikolay
e-mail [email protected]
phone +7-902-272-65-35
Telegram @sbwerewolf

Chat with me via messenger

stopwatch's People

Contributors

sbwerewolf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.