Skip to content

Instantly share code, notes, and snippets.

@mdwheele
Last active July 9, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwheele/e4dce7adb7a4f523011c21de6b29f001 to your computer and use it in GitHub Desktop.
Save mdwheele/e4dce7adb7a4f523011c21de6b29f001 to your computer and use it in GitHub Desktop.

Benchmark

This is a benchmark for https://wiki.php.net/rfc/friend-classes that attempts to approximate performance delta between master and feature/friend-classes-poc.

Both benchmarks execute a script that attempts access of a target property 100,000 times. We sample 100 executions of this script and average to compare.

Compilation

make clean
make distclean
./vcsrepo
./buildconf
./configure --enable-cli --enable-debug
make -j9

Control

As this is functionality that doesn't exist, we can only compare difference in property access (in the worst case) as well as clock time to parse / manage "friend" data on zend_class_entry.

The following script is executed to measure access time for a public property.

<?php

class Readable {
  public $property = 'foo';
}

class Reader {
  public function read(Readable $object) {
    $foo = $object->property;
  }
}

$readable = new Readable();
$reader = new Reader();

// Time the following...
$start = microtime();
for ($i = 0; $i < 100000; $i++) {
  $reader->read($readable);
}
echo (microtime() - $start) . PHP_EOL;

This is master running for n in {1..100}; do sapi/cli/php ~/control.php; done.

Average: 0.01874479592 seconds

Experiment

We are timing protected access for a friend class as well as time it takes to attach "friend" data to NotReadable's zend_class_entry.

<?php

class NotReadable {
  friend Reader;

  protected $property = 'foo';
}

class Reader {
  public function read(NotReadable $object) {
    $foo = $object->property;
  }
}

$not_readable = new NotReadable();
$reader = new Reader();

// Time the following...
$start = microtime();
for ($i = 0; $i < 100000; $i++) {
  $reader->read($not_readable);
}
echo (microtime() - $start) . PHP_EOL;

This is feature/friend-classes-poc running for n in {1..100}; do sapi/cli/php ~/experiment.php; done.

Average: 0.01917070103 seconds

Result

There is a 0.000425905 difference in runtime (experiment slower). Given the small sample size for this benchmark, I would claim "no noticeable difference" in performance between the two branches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment