PHP code example of macocci7 / php-benchmark

1. Go to this page and download the library: Download macocci7/php-benchmark 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/ */

    

macocci7 / php-benchmark example snippets


    

    ci7\PhpBenchmark\Benchmark;

    $iteration = 10000;

    $haystack = 'GPSAltitude';
    $needle = 'GPS';

    $name = 'str_starts_with()';
    $callback = function ($haystack, $needle) {
        if (str_starts_with($haystack, $needle)) {
        }
    };
    $params = [ 'haystack' => $haystack, 'needle' => $needle, ];

    $result = Benchmark::code($name, $callback, $params, $iteration);
    Benchmark::stdout($result);
    

    

    ci7\PhpBenchmark\Benchmark;

    $iteration = 10000;

    $params = [
        'haystack' => 'GPSAltitude',
        'needle' => 'GPS',
        'pattern' => sprintf("/^%s/", 'GPS'),
    ];

    $sort = true;
    $desc = false;

    $callbacks = [
        'str_starts_with()' => function ($haystack, $needle, $pattern) {
            if (str_starts_with($haystack, $needle)) {
            }
        },
        'strpos()' => function ($haystack, $needle, $pattern) {
            if (strpos($haystack, $needle)) {
            }
        },
        'strpbrk()' => function ($haystack, $needle, $pattern) {
            if (strpbrk($haystack, $needle)) {
            }
        },
        'strncmp()' => function ($haystack, $needle, $pattern) {
            if (0 === strncmp($haystack, $needle, 3)) {
            }
        },
        'strstr()' => function ($haystack, $needle, $pattern) {
            if (strstr($haystack, $needle)) {
            }
        },
        'preg_match()' => function ($haystack, $needle, $pattern) {
            if (preg_match($pattern, $haystack)) {
            }
        },
        'strcmp() + substr()' => function ($haystack, $needle, $pattern) {
            if (0 === strcmp(substr($haystack, 0, 3), $needle)) {
            }
        },
        'substr_compare()' => function ($haystack, $needle, $pattern) {
            if (0 === substr_compare($haystack, $needle, 0, 3)) {
            }
        },
    ];

    $results = Benchmark::codes($callbacks, $params, $iteration, $sort, $desc);
    Benchmark::stdout($results);
    
bash
    composer