Skip to content

Instantly share code, notes, and snippets.

@ircmaxell
Last active December 18, 2015 12:19
Show Gist options
  • Save ircmaxell/5782139 to your computer and use it in GitHub Desktop.
Save ircmaxell/5782139 to your computer and use it in GitHub Desktop.
Compare Modified GC

To set this up, have 2 builds of PHP, one using the "master" branch, and one using my refactor branch. Configure the paths to the SAPI at the top of benchmark.php.

Note that it's hard-coded to look for php-src/Zend/bench.php, php-src/Zend/micro_bench.php and gc_test.php.

<?php
function err($msg) { file_put_contents('php://stderr', $msg); }
$sapis = array(
'core' => 'php-src2/sapi/cli/php',
'modified' => 'php-src/sapi/cli/php',
);
$results = array();
foreach ($sapis as $name => $sapi) {
err("\nRunning $name\n");
$bm = new Benchmarker($sapi);
$result = $bm->benchmark();
$results[$name] = array();
foreach ($result as $test => $values) {
$results[$name][$test] = array(
'min' => min($values),
'max' => max($values),
'average' => array_sum($values) / count($values),
);
}
}
foreach ($results['core'] as $test => $result) {
$modres = $results['modified'][$test];
echo "\n$test\n";
echo " Core: {$result['min']} - {$result['average']} - {$result['max']}\n";
echo " Modified: {$modres['min']} - {$modres['average']} - {$modres['max']}\n";
echo " Diff: " . number_format((($result['min'] - $modres['min']) / $result['min']) * 100, 5) . '% - ';
echo number_format((($result['average'] - $modres['average']) / $result['max']) * 100, 5) . '% - ';
echo number_format((($result['max'] - $modres['max']) / $result['max']) * 100, 5) . "\n";
}
class Benchmarker {
protected $sapi;
protected $runs = 10;
public function __construct($sapi) {
$this->sapi = $sapi;
}
public function benchmark() {
$results = array_merge(
$this->benchmarkFull(),
$this->benchmarkMicro(),
$this->benchmarkGC()
);
return $results;
}
public function benchmarkFull() {
$cmd = $this->sapi . ' php-src/Zend/bench.php';
$results = array();
for ($i = 0; $i < $this->runs; $i++) {
err('.');
$result = shell_exec($cmd);
$tests = explode("\n", $result);
foreach ($tests as $test) {
if (empty($test) || $test[0] === '-' || '' == trim($test)) continue;
list ($name, $value) = preg_split("(\s+)", trim($test));
$name = 'Full: ' . $name;
if (!isset($results[$name])) {
$results[$name] = array();
}
$results[$name][] = (float) $value;
}
}
return $results;
}
public function benchmarkGC() {
$cmd = $this->sapi . ' -d memory_limit=512M php-src/test.php';
$results = array('GC' => array());
for ($i = 0; $i < $this->runs; $i++) {
err('-');
$m = microtime(true);
shell_exec($cmd);
$e = microtime(true);
$results['GC'][] = $e - $m;
}
return $results;
}
public function benchmarkMicro() {
$cmd = $this->sapi . ' php-src/Zend/micro_bench.php';
$results = array();
for ($i = 0; $i < $this->runs; $i++) {
err('_');
$result = shell_exec($cmd);
$tests = explode("\n", $result);
foreach ($tests as $test) {
if (empty($test) || $test[0] === '-' || '' == trim($test)) continue;
list ($name, $value) = preg_split("(\s+(?=\d+\.))", trim($test));
if (!$name || !$value) {
var_dump($test, $result);
die();
}
$name = 'Micro: ' . $name;
if (!isset($results[$name])) {
$results[$name] = array();
}
$results[$name][] = (float) $value;
}
}
return $results;
}
}
<?php
$counter = 0;
$a = array(&$a);
function fill (&$a, $i = 3, &$counter) {
if ($counter++ > 200000) die();
if (!$i--) return;
foreach ($a as &$tmp) {
$tmp = array($tmp, &$a);
fill($tmp, $i, $counter);
}
}
fill($a, 3, $counter);
Each test ran a total of 10 times.
Diff percentages are "Modified" is faster. So a positive percentage is a faster result in the modified algorithm, a negative percentage is a slower result in the modified algorithm.
Full: simple
Core: 0.104 - 0.1053 - 0.109
Modified: 0.103 - 0.105 - 0.107
Diff: 0.96154% - 0.27523% - 1.83486
Full: simplecall
Core: 0.099 - 0.103 - 0.115
Modified: 0.101 - 0.1046 - 0.114
Diff: -2.02020% - -1.39130% - 0.86957
Full: simpleucall
Core: 0.098 - 0.1032 - 0.123
Modified: 0.097 - 0.1004 - 0.109
Diff: 1.02041% - 2.27642% - 11.38211
Full: simpleudcall
Core: 0.1 - 0.1032 - 0.107
Modified: 0.1 - 0.1032 - 0.111
Diff: 0.00000% - 0.00000% - -3.73832
Full: mandel
Core: 0.272 - 0.293 - 0.366
Modified: 0.256 - 0.263 - 0.283
Diff: 5.88235% - 8.19672% - 22.67760
Full: mandel2
Core: 0.284 - 0.2924 - 0.298
Modified: 0.279 - 0.2901 - 0.31
Diff: 1.76056% - 0.77181% - -4.02685
Full: ackermann(7)
Core: 0.114 - 0.1181 - 0.123
Modified: 0.114 - 0.1164 - 0.121
Diff: 0.00000% - 1.38211% - 1.62602
Full: ary(50000)
Core: 0.019 - 0.0204 - 0.023
Modified: 0.019 - 0.02 - 0.021
Diff: 0.00000% - 1.73913% - 8.69565
Full: ary2(50000)
Core: 0.018 - 0.0187 - 0.019
Modified: 0.019 - 0.0191 - 0.02
Diff: -5.55556% - -2.10526% - -5.26316
Full: ary3(2000)
Core: 0.144 - 0.1491 - 0.156
Modified: 0.145 - 0.1504 - 0.156
Diff: -0.69444% - -0.83333% - 0.00000
Full: fibo(30)
Core: 0.303 - 0.3114 - 0.323
Modified: 0.325 - 0.3327 - 0.341
Diff: -7.26073% - -6.59443% - -5.57276
Full: hash1(50000)
Core: 0.032 - 0.0331 - 0.035
Modified: 0.032 - 0.0329 - 0.034
Diff: 0.00000% - 0.57143% - 2.85714
Full: hash2(500)
Core: 0.035 - 0.0362 - 0.041
Modified: 0.034 - 0.036 - 0.039
Diff: 2.85714% - 0.48780% - 4.87805
Full: heapsort(20000)
Core: 0.081 - 0.0851 - 0.088
Modified: 0.079 - 0.0837 - 0.098
Diff: 2.46914% - 1.59091% - -11.36364
Full: matrix(20)
Core: 0.087 - 0.09 - 0.096
Modified: 0.086 - 0.0902 - 0.095
Diff: 1.14943% - -0.20833% - 1.04167
Full: nestedloop(12)
Core: 0.17 - 0.1767 - 0.196
Modified: 0.171 - 0.1763 - 0.2
Diff: -0.58824% - 0.20408% - -2.04082
Full: sieve(30)
Core: 0.089 - 0.0932 - 0.112
Modified: 0.095 - 0.0971 - 0.103
Diff: -6.74157% - -3.48214% - 8.03571
Full: strcat(200000)
Core: 0.011 - 0.0116 - 0.014
Modified: 0.008 - 0.0108 - 0.013
Diff: 27.27273% - 5.71429% - 7.14286
Micro: empty_loop
Core: 0.106 - 0.1081 - 0.113
Modified: 0.104 - 0.1149 - 0.171
Diff: 1.88679% - -6.01770% - -51.32743
Micro: func()
Core: 0.303 - 0.3092 - 0.322
Modified: 0.296 - 0.3038 - 0.316
Diff: 2.31023% - 1.67702% - 1.86335
Micro: undef_func()
Core: 0.307 - 0.3156 - 0.324
Modified: 0.302 - 0.3111 - 0.328
Diff: 1.62866% - 1.38889% - -1.23457
Micro: int_func()
Core: 0.329 - 0.3361 - 0.357
Modified: 0.258 - 0.2656 - 0.274
Diff: 21.58055% - 19.74790% - 23.24930
Micro: $x = self::$x
Core: 0.228 - 0.2363 - 0.269
Modified: 0.221 - 0.2349 - 0.296
Diff: 3.07018% - 0.52045% - -10.03717
Micro: self::$x = 0
Core: 0.22 - 0.2265 - 0.231
Modified: 0.317 - 0.3276 - 0.383
Diff: -44.09091% - -43.76623% - -65.80087
Micro: isset(self::$x)
Core: 0.205 - 0.2126 - 0.234
Modified: 0.199 - 0.2105 - 0.264
Diff: 2.92683% - 0.89744% - -12.82051
Micro: empty(self::$x)
Core: 0.221 - 0.23 - 0.257
Modified: 0.211 - 0.2301 - 0.275
Diff: 4.52489% - -0.03891% - -7.00389
Micro: $x = Foo::$x
Core: 0.201 - 0.2191 - 0.337
Modified: 0.194 - 0.2022 - 0.213
Diff: 3.48259% - 5.01484% - 36.79525
Micro: Foo::$x = 0
Core: 0.193 - 0.1977 - 0.207
Modified: 0.279 - 0.2888 - 0.301
Diff: -44.55959% - -44.00966% - -45.41063
Micro: isset(Foo::$x)
Core: 0.178 - 0.1851 - 0.2
Modified: 0.175 - 0.1947 - 0.264
Diff: 1.68539% - -4.80000% - -32.00000
Micro: empty(Foo::$x)
Core: 0.191 - 0.2052 - 0.224
Modified: 0.186 - 0.2024 - 0.222
Diff: 2.61780% - 1.25000% - 0.89286
Micro: self::f()
Core: 0.329 - 0.338 - 0.356
Modified: 0.32 - 0.3307 - 0.344
Diff: 2.73556% - 2.05056% - 3.37079
Micro: Foo::f()
Core: 0.293 - 0.3011 - 0.314
Modified: 0.288 - 0.2994 - 0.309
Diff: 1.70648% - 0.54140% - 1.59236
Micro: $x = $this->x
Core: 0.204 - 0.2116 - 0.218
Modified: 0.202 - 0.2089 - 0.217
Diff: 0.98039% - 1.23853% - 0.45872
Micro: $this->x = 0
Core: 0.279 - 0.2883 - 0.321
Modified: 0.268 - 0.2765 - 0.289
Diff: 3.94265% - 3.67601% - 9.96885
Micro: $this->x += 2
Core: 0.215 - 0.2225 - 0.243
Modified: 0.208 - 0.2153 - 0.224
Diff: 3.25581% - 2.96296% - 7.81893
Micro: ++$this->x
Core: 0.187 - 0.1938 - 0.203
Modified: 0.184 - 0.1893 - 0.201
Diff: 1.60428% - 2.21675% - 0.98522
Micro: $this->x++
Core: 0.264 - 0.2859 - 0.304
Modified: 0.209 - 0.2195 - 0.258
Diff: 20.83333% - 21.84211% - 15.13158
Micro: $this->x--
Core: 0.207 - 0.2216 - 0.292
Modified: 0.204 - 0.2126 - 0.224
Diff: 1.44928% - 3.08219% - 23.28767
Micro: isset($this->x)
Core: 0.196 - 0.2011 - 0.21
Modified: 0.19 - 0.1977 - 0.214
Diff: 3.06122% - 1.61905% - -1.90476
Micro: empty($this->x)
Core: 0.206 - 0.2143 - 0.238
Modified: 0.203 - 0.2101 - 0.224
Diff: 1.45631% - 1.76471% - 5.88235
Micro: $this->f()
Core: 0.336 - 0.3479 - 0.379
Modified: 0.327 - 0.3354 - 0.352
Diff: 2.67857% - 3.29815% - 7.12401
Micro: $x = Foo::TEST
Core: 0.258 - 0.2686 - 0.292
Modified: 0.191 - 0.1952 - 0.201
Diff: 25.96899% - 25.13699% - 31.16438
Micro: new Foo()
Core: 0.828 - 0.8442 - 0.886
Modified: 0.685 - 0.7015 - 0.729
Diff: 17.27053% - 16.10609% - 17.72009
Micro: $x = TEST
Core: 0.152 - 0.1571 - 0.176
Modified: 0.15 - 0.155 - 0.165
Diff: 1.31579% - 1.19318% - 6.25000
Micro: $x = $_GET
Core: 0.318 - 0.3218 - 0.327
Modified: 0.216 - 0.2212 - 0.231
Diff: 32.07547% - 30.76453% - 29.35780
Micro: $x = $GLOBALS['v']
Core: 0.283 - 0.2885 - 0.301
Modified: 0.28 - 0.293 - 0.371
Diff: 1.06007% - -1.49502% - -23.25581
Micro: $x = $hash['v']
Core: 0.209 - 0.2173 - 0.225
Modified: 0.207 - 0.2166 - 0.278
Diff: 0.95694% - 0.31111% - -23.55556
Micro: $x = $str[0]
Core: 0.303 - 0.3183 - 0.333
Modified: 0.299 - 0.3101 - 0.333
Diff: 1.32013% - 2.46246% - 0.00000
Micro: $x = $a ?: null
Core: 0.297 - 0.3037 - 0.319
Modified: 0.202 - 0.2077 - 0.212
Diff: 31.98653% - 30.09404% - 33.54232
Micro: $x = $f ?: tmp
Core: 0.271 - 0.2773 - 0.295
Modified: 0.264 - 0.267 - 0.27
Diff: 2.58303% - 3.49153% - 8.47458
Micro: $x = $f ? $f : $a
Core: 0.347 - 0.3533 - 0.359
Modified: 0.211 - 0.2153 - 0.22
Diff: 39.19308% - 38.44011% - 38.71866
Micro: $x = $f ? $f : tmp
Core: 0.353 - 0.3574 - 0.362
Modified: 0.259 - 0.2703 - 0.29
Diff: 26.62890% - 24.06077% - 19.88950
Full: Total
Core: 2.094 - 2.1429 - 2.278
Modified: 2.089 - 2.1323 - 2.208
Diff: 0.23878% - 0.46532% - 3.07287
Micro: Total
Core: 9.309 - 9.4995 - 9.878
Modified: 8.62 - 8.8179 - 9.234
Diff: 7.40144% - 6.90018% - 6.51954
GC
Core: 0.5937340259552 - 0.60687265396118 - 0.62824201583862
Modified: 0.43920111656189 - 0.44599950313568 - 0.47265577316284
Diff: 26.02730% - 25.60688% - 24.76534
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment