PHP code example of petrknap / profiler

1. Go to this page and download the library: Download petrknap/profiler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

petrknap / profiler example snippets


namespace PetrKnap\Profiler;

$profiling = Profiling::start();
// do something
$profile = $profiling->finish();

printf('It took %.1f s to do something.', $profile->getDuration());

namespace PetrKnap\Profiler;

function something(ProfilerInterface $profiler): string {
    // do something without profiling
    return $profiler->profile(function (): string {
        // do something
        return 'something';
    })->process(fn (ProfileInterface $profile) => printf(
        'It took %.1f s to do something.',
        $profile->getDuration(),
    ));
}

namespace PetrKnap\Profiler;

echo something(new Profiler());
echo something(new NullProfiler());

namespace PetrKnap\Profiler;

echo (new Profiler())->profile(function (ProfilerInterface $profiler): string {
    // do something before something
    return something($profiler);
})->process(fn (ProfileInterface $profile) => printf(
    'It took %.1f s to do something before something and something, there are %d children profiles.',
    $profile->getDuration(),
    count($profile->getChildren()),
));