Skip to content

Instantly share code, notes, and snippets.

@shahmal1yev
Created July 29, 2024 16:16
Show Gist options
  • Save shahmal1yev/e76382c392c8f8d7f50edc6496ce31b4 to your computer and use it in GitHub Desktop.
Save shahmal1yev/e76382c392c8f8d7f50edc6496ce31b4 to your computer and use it in GitHub Desktop.
LinkedList vs Array in PHP across different versions: 7.4.33 and 8.3.9
<?php
namespace Tests\LinkedList\Unit;
ini_set('memory_limit', '512M');
use PHPUnit\Framework\TestCase;
use SplDoublyLinkedList;
class LinkedListTest extends TestCase
{
public function testLinkedListVsArray(): void
{
$iterator = 1;
$this->assertGreaterThan(0, $iterator);
$data = new class ("name", "surname") {};
$this->report();
$this->report();
$this->report(sprintf("PHP Version: %s", phpversion()));
$this->report(sprintf("Iterator: %s", number_format($iterator)));
$this->report();
$this->report();
$arrayFillPerformance = $this->measure(
'Array Fill Performance:',
array(),
$iterator,
fn (&$collection) => array_push($collection, $data)
);
$linkedListFillPerformance = $this->measure(
'LinkedList Fill Performance:',
new SplDoublyLinkedList(),
$iterator,
fn ($collection) => $collection->push($data)
);
$this->report(
$linkedListFillPerformance['performance'] > $arrayFillPerformance['performance']
? "*** Array faster than LinkedList. ***"
: "*** LinkedList faster than Array. ***"
);
$this->report();
$this->report();
$arrayEmptyPerformance = $this->measure(
'Array Empty Performance:',
$arrayFillPerformance['collection'],
$iterator,
fn (&$collection) => array_pop($collection)
);
$linkedListEmptyPerformance = $this->measure(
'LinkedList Empty Performance:',
$linkedListFillPerformance['collection'],
$iterator,
fn ($collection) => $collection->pop()
);
$this->report(
$linkedListEmptyPerformance['performance'] > $arrayEmptyPerformance['performance']
? "*** Array faster than LinkedList. ***"
: "*** LinkedList faster than Array. ***"
);
$this->report();
$this->report();
}
private function measure(string $title, $collection, $iterator, $action): array
{
$results = $this->measurePerformance(
$collection,
$iterator,
$action
);
$this->report($title);
$this->report();
$this->reportOf($results);
$this->report();
return $results;
}
private function report(string $message = null): void
{
echo $message . PHP_EOL;
}
private function reportOf(array $performanceResult): void
{
$this->report(sprintf("In seconds (rounded): %s", round($performanceResult['performance'])));
$this->report(sprintf("In seconds: %s", $performanceResult['performance']));
$this->report(sprintf("Memory usage (formatted): %s %s", number_format($performanceResult['memoryUsage']), "bytes"));
$this->report(sprintf("Memory usage: %s", $performanceResult['memoryUsage']));
}
private function measurePerformance($collection, int $iterator, callable $action): array
{
$startTime = microtime(true);
$startMemory = memory_get_usage(true);
foreach(range(0, $iterator) as $i) {
$action($collection);
}
$endTime = microtime(true);
$endMemory = memory_get_usage(true);
return [
'performance' => $endTime - $startTime,
'memoryUsage' => $endMemory - $startMemory,
'collection' => $collection,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment