Skip to content

Instantly share code, notes, and snippets.

@atastycookie
Created October 20, 2020 16:47
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 atastycookie/6d25d8b03942047b6ba3e3209a609a6d to your computer and use it in GitHub Desktop.
Save atastycookie/6d25d8b03942047b6ba3e3209a609a6d to your computer and use it in GitHub Desktop.
Сравнение производительности и использования ресурсов strict и non-strict кода на PHP7.4 и PHP8 для рандомного массива и его пузырьковой сортировки.
<?php
$count = 1000;
$minNumber = 0;
$maxNumber = 666;
$array = getRandomNumbers($count, $minNumber, $maxNumber);
$sorter = new BubbleSorter($array);
$sortedArray = $sorter->sort();
echo implode(' ', $sortedArray) . PHP_EOL;
function getRandomNumbers($count, $min, $max)
{
$generator = new NumberGenerator($min, $max);
return $generator->getNumbers($count)->getResult();
}
class NumberGenerator
{
private $min;
private $max;
private $result = [];
public function __construct($min, $max)
{
$this->min = $min;
$this->max = $max;
}
public function getNumbers($count)
{
$this->result = [];
if (!empty($count)) {
for ($i = 0; $i < $count; $i++) {
$this->result[] = random_int($this->min, $this->max);
}
}
return $this;
}
public function getResult()
{
return $this->result;
}
}
class BubbleSorter
{
private $array = [];
public function __construct($array)
{
$this->array = $array;
}
public function sort()
{
if (!empty($this->array)) {
do {
$swapped = false;
for ($i = 0, $count = count($this->array) - 1; $i < $count; $i++) {
if ($this->array[$i] > $this->array[$i + 1]) {
list($this->array[$i + 1], $this->array[$i]) = [$this->array[$i], $this->array[$i + 1]];
$swapped = true;
}
}
} while($swapped);
}
return $this->array;
}
}
<?php
declare(strict_types = 1);
(int) $count = 1000;
(int) $minNumber = 0;
(int) $maxNumber = 100;
(array) $array = getRandomNumbers($count, $minNumber, $maxNumber);
echo implode(' ', $array) . PHP_EOL;
(object) $sorter = new BubbleSorter($array);
(array) $sortedArray = $sorter->sort();
echo implode(' ', $sortedArray) . PHP_EOL;
function getRandomNumbers(int $count, int $min, int $max) : array
{
(object) $generator = new NumberGenerator($min, $max);
return $generator->getNumbers($count)->getResult();
}
class NumberGenerator
{
private int $min;
private int $max;
private array $result = [];
public function __construct(int $min, int $max)
{
$this->min = $min;
$this->max = $max;
}
public function getNumbers(int $count) : self
{
$this->result = [];
if (!empty($count)) {
for ((int) $i = 0; $i < $count; $i++) {
$this->result[] = random_int($this->min, $this->max);
}
}
return $this;
}
public function getResult() : array
{
return $this->result;
}
}
class BubbleSorter
{
private array $array = [];
public function __construct(array $array)
{
$this->array = $array;
}
public function sort() : array
{
if (!empty($this->array)) {
do {
(bool) $swapped = false;
for ((int) $i = 0, (int) $count = count($this->array) - 1; $i < $count; $i++) {
if ($this->array[$i] > $this->array[$i + 1]) {
list($this->array[$i + 1], $this->array[$i]) = [$this->array[$i], $this->array[$i + 1]];
$swapped = true;
}
}
} while($swapped);
}
return $this->array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment