Git Product home page Git Product logo

chrono's Introduction

⏱ Chrono

The easiest way to measure PHP code execution time.

Introduction

Every time I need to benchmark something, I find myself re-inventing the wheel and do manual time calculations. This hurts my productivity. I wanted a simple little package that is very easy to use that I can go to whenever I need to measure execution time in PHP.

  • Easy to remember
  • 100% tested
  • Framework agnostic
  • No dependencies

Installation

composer require angle/chrono

Documentation

This statement is implicitly used above any of the following examples:

use Angle\Chrono;

The shortest way to benchmark your code is to use the benchmark method:

echo Chrono::benchmark(function () {
    sleep(1);
});

Output:

Time: 1 second (1002 ms)

By default, it invokes the 'meter' method which pretty-prints the output. Optionally, you can pass a second parameter to invoke any available formatting methods (ms, seconds, minutes):

echo Chrono::benchmark(function () {
    sleep(1);
}, 'ms');

Output:

1002

Manual usage:

Chrono::start();

// Do something...

echo Chrono::stop(); // 2.42424242 (seconds)

To get formatted output, use the 'meter' method:

Chrono::start();

// Compute stuff

echo Chrono::meter(); // Time: 0.42 seconds (4242 ms)

You can access elapsed time whenever you need, in various formats:

echo Chrono::elapsed(); // 0.42424242
echo Chrono::seconds(); // 0.43
echo Chrono::ms(); // 4243

You can pause and resume the chronometer:

Chrono::start();

sleep(1);

Chrono::pause();

sleep(3); // Will be ignored

Chrono::resume();

sleep(1);

print Chrono::meter(); // Time: 2 seconds (2001 ms)

If you are running many benchmarks, it may be a good idea to add a description to them:

Chrono::describe('Query with joints');
Chrono::start();

// Run the query

print Chrono::meter();

Output:

Query with joints | 1.43 seconds (1424 ms)

Warning: if you prefer to use the shorthand benchmark method, make sure to call the describe method within your benchmark:

Chrono::benchmark(function () {
    Chrono::describe('Task');

    // Do your thing
});

For a more detailed overview, please refer to the test suite.

Credits

This implementation is inspired by David Walsh's timer class.

Contributing

Improvements are welcome! Feel free to submit pull requests.

Licence

MIT

Copyright © 2019 Angle Software

chrono's People

Contributors

benvilliere avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

benvilliere

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.