Skip to content

Instantly share code, notes, and snippets.

@drealecs
Last active March 7, 2019 13:24
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 drealecs/d5263c8c092f3faa565e49329c009b96 to your computer and use it in GitHub Desktop.
Save drealecs/d5263c8c092f3faa565e49329c009b96 to your computer and use it in GitHub Desktop.
PHP 7.3 non recursive GC performance comparison
<?php
class Node
{
/** @var Node */
public $previous;
/** @var Node */
public $next;
}
function test() {
$firstNode = new Node();
$firstNode->previous = $firstNode;
$firstNode->next = $firstNode;
$circularDoublyLinkedList = $firstNode;
for ($i = 0; $i < 30000; $i++) {
$currentNode = $circularDoublyLinkedList;
$nextNode = $circularDoublyLinkedList->next;
$newNode = new Node();
$newNode->previous = $currentNode;
$currentNode->next = $newNode;
$newNode->next = $nextNode;
$nextNode->previous = $newNode;
$circularDoublyLinkedList = $nextNode;
}
return $circularDoublyLinkedList;
}
ini_set('memory_limit', '1G');
$start = microtime(true);
echo "Start running\n";
$all = [];
foreach (range(1, 250) as $i) {
$all[] = test();
}
$step1 = microtime(true);
echo "Running = " . ($step1 - $start) . "\n";
echo "Memory = " . (memory_get_usage()/1024/1024) . "\n";
echo "Peak memory = " . (memory_get_peak_usage()/1024/1024) . "\n";
gc_collect_cycles();
$step2 = microtime(true);
echo "Running GC without collecting = " . ($step2 - $step1) . "\n";
echo "Memory = " . (memory_get_usage()/1024/1024) . "\n";
echo "Peak memory = " . (memory_get_peak_usage()/1024/1024) . "\n";
$all = null;
gc_collect_cycles();
$step3 = microtime(true);
echo "Running GC with collecting = " . ($step3 - $step2) . "\n";
echo "Memory = " . (memory_get_usage()/1024/1024) . "\n";
echo "Peak memory = " . (memory_get_peak_usage()/1024/1024) . "\n";
echo "Total = " . ($step3 - $start) . "\n";
Start running
Running = 2.8742311000824
Memory = 636.60913085938
Peak memory = 636.62090301514
Running GC without collecting = 0.012032032012939
Memory = 636.60913085938
Peak memory = 636.62090301514
Running GC with collecting = 1.4210410118103
Memory = 64.373695373535
Peak memory = 636.62090301514
Total = 4.3073041439056
Start running
Running = 2.4045481681824
Memory = 636.60913085938
Peak memory = 636.62090301514
Running GC without collecting = 0.0075688362121582
Memory = 636.60913085938
Peak memory = 636.62090301514
Running GC with collecting = 0.80605506896973
Memory = 64.373695373535
Peak memory = 636.62090301514
Total = 3.2181720733643
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment