Skip to content

Instantly share code, notes, and snippets.

@nikic nikic/bench_sort.php Secret
Last active May 12, 2020

Embed
What would you like to do?
<?php
$numElems = 100000;
$numRepeats = 100;
foreach ([0.0, 0.01, 0.05, 0.1, 0.2, 0.5, 1.0] as $duplicateFraction) {
echo "$duplicateFraction: ";
$uniqueElems = (int) round($numElems * (1 - $duplicateFraction));
$origArray = array_pad(range(0, $uniqueElems), $numElems, 0);
$t = microtime(true);
for ($i = 0; $i < $numRepeats; $i++) {
$array = $origArray;
shuffle($array);
sort($array);
}
var_dump(microtime(true) - $t);
}
UNSTABLE
0.00: float(1.5692839622497559)
0.01: float(1.572052001953125)
0.05: float(1.5424528121948242)
0.10: float(1.5431289672851562)
0.20: float(1.4708871841430664)
0.50: float(1.2815279960632324)
1.00: float(0.9397969245910645)
STABLE
0.00: float(1.5857720375061035)
0.01: float(1.577707052230835)
0.05: float(1.581636905670166)
0.10: float(1.588440179824829)
0.20: float(1.600801944732666)
0.50: float(1.5892488956451416)
1.00: float(1.0026319026947021)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.