2017 © Pedro Pelรกez
 

library phpcb

PHP Code Benchmark

image

smuuf/phpcb

PHP Code Benchmark

  • Friday, October 6, 2017
  • by smuuf
  • Repository
  • 4 Watchers
  • 7 Stars
  • 500 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 2 % Grown

The README.md

PHP tests, (*1)

phpcb (php code benchmark)

phpcb is a very simple and very lightweight tool for speed benchmarking of various little pieces of PHP code, written in PHP, of course., (*2)

Why

phpcb is meant to be used in those situations when there are multiple ways of how to do something - and you know all will have the exact same result - but you just can't decide which would ultimately be the best (meaning "fastest") to use., (*3)

Requirements

  • PHP 7.4+
  • BCMath Arbitrary Precision Mathematics library for PHP; shouldn't be a problem, since it is commonly shipped with PHP itself.

Installation

composer require --dev smuuf/-hpcb

Usage

Write your microbenchmarks in a some file. For example mymicrobench.php (here placed in phpcb's root directory, so it's clear we rquire Composer's autoload file from correct place):, (*4)

<?php

require __DIR__ . '/../vendor/autoload.php';
$bench = new \Smuuf\Phpcb\PhpBenchmark;

const COUNT = 100;

$bench->addBench(function() {
    for ($i = 1; $i <= COUNT; $i++) {}
});

$bench->addBench(function() {
    for ($i = COUNT; $i > 0; $i--) {}
});

$bench->addBench(function() {
    for ($i = COUNT; $i--;) {}
});

$bench->addBench(function() {
    for ($i = -COUNT; $i++;) {}
});

$bench->run();

And then run it:, (*5)

$ php ./mymicrobench.php

And observe results:, (*6)

โ–ˆ PHP Code Benchmark (phpcb)
โ–ˆ PHP 7.4.27

Engine used: Chaotic Engine
Total time: 1.3220 sec
Iterations: 1 000 000

โ–ˆโ–ˆ 2. Score: 100.00, 0.2660 sec
โ”Œ
โ”‚ $bench->addBench(function() {
โ”‚       for ($i = COUNT; $i > 0; $i--) {}
โ”‚ });
โ””

โ–ˆโ–ˆ 1. Score: 86.54, 0.3074 sec, 1.16x slower
โ”Œ
โ”‚ $bench->addBench(function() {
โ”‚       for ($i = 1; $i <= COUNT; $i++) {}
โ”‚ });
โ””

โ–ˆโ–ˆ 4. Score: 71.13, 0.3740 sec, 1.41x slower
โ”Œ
โ”‚ $bench->addBench(function() {
โ”‚       for ($i = -COUNT; $i++;) {}
โ”‚ });
โ””

โ–ˆโ–ˆ 3. Score: 71.02, 0.3746 sec, 1.41x slower
โ”Œ
โ”‚ $bench->addBench(function() {
โ”‚       for ($i = COUNT; $i--;) {}
โ”‚ });
โ””

The Versions

06/10 2017

dev-master

9999999-dev

PHP Code Benchmark

  Sources   Download