Skip to content

Instantly share code, notes, and snippets.

@fesor

fesor/bench.php

Created Oct 17, 2014
Embed
What would you like to do?
Experements...
<?php
define('REPEAT_BENCH', 4);
define('REPEAT_EXEC', 5);
function array_merge_sorted($first, $second)
{
$out = [];
for (
$f = 0, $s = 0,
$firstLength = count($first),
$secondLength = count($second);
$f < $firstLength || $s < $secondLength;
) {
if (!isset($first[$f])) {
$out[] = $second[$s];
++$s;
} elseif (!isset($second[$s])) {
$out[] = $first[$f];
++$f;
} elseif ($first[$f] > $second[$s]) {
$out[] = $second[$s];
++$s;
} else {
$out[] = $first[$f];
++$f;
}
}
return $out;
}
function merge_ordered($a, $b) {
// let a will be longest array
$aLen = count($a);
$bLen = count($b);
$resultLen = $aLen + $bLen;
$result = new SplFixedArray($resultLen);
for(
$i = 0, $aIdx=0, $bIdx=0;
$i < $resultLen;
$i++
) {
if ($aIdx === $aLen || ($bIdx < $bLen && $a[$aIdx] > $b[$bIdx])) {
$result[$i] = $b[$bIdx++];
} else {
$result[$i] = $a[$aIdx++];
}
}
return $result;
}
function merge_ordered3($a, $b) {
// let a will be longest array
$aLen = count($a);
$bLen = count($b);
$resultLen = $aLen + $bLen;
$result = new SplFixedArray($resultLen);
for(
$i = 0, $aIdx=0, $bIdx=0;
$i < $resultLen;
$i++
) {
if ($aIdx === $aLen || ($bIdx < $bLen && $a[$aIdx] > $b[$bIdx])) {
$result[$i] = $b[$bIdx++];
} else {
$result[$i] = $a[$aIdx++];
}
}
return $result->toArray();
}
function merge_ordered2($a, $b) {
// let a will be longest array
$aLen = count($a);
$bLen = count($b);
$resultLen = $aLen + $bLen;
$result = [];
for(
$i = 0, $aIdx=0, $bIdx=0;
$i < $resultLen;
$i++
) {
if ($aIdx === $aLen || ($bIdx < $bLen && $a[$aIdx] > $b[$bIdx])) {
$result[] = $b[$bIdx++];
} else {
$result[] = $a[$aIdx++];
}
}
return $result;
}
function native_merge($a, $b) {
$r = array_merge($a, $b);
sort($r);
return $r;
}
function benchmark($description, $callback) {
$averageExecutionTime = 0;
$minExecutionTime = PHP_INT_MAX;
$maxExecutionTime = 0;
for($i = 0;$i<REPEAT_BENCH;$i++) {
$executionTime = 0;
for($j = 0;$j < REPEAT_EXEC;$j++) {
$executionTime += $callback();
}
$executionTime /= REPEAT_EXEC;
if ($executionTime > $maxExecutionTime) {
$maxExecutionTime = $executionTime;
}
if ($executionTime < $minExecutionTime) {
$minExecutionTime = $executionTime;
}
$averageExecutionTime += $executionTime;
}
$averageExecutionTime /= REPEAT_BENCH;
echo sprintf("%s:\n-----------------\n", $description);
echo sprintf("Average execution time: %d ms\n", $averageExecutionTime*1000);
echo sprintf("Min execution time: %d ms\n", $minExecutionTime*1000);
echo sprintf("Max execution time: %d ms\n\n", $maxExecutionTime*1000);
}
echo "Generating test data...";
$a = range(2, 1000000, 2);
$b = range(2, 2000000, 4);
echo " done.\n\n";
benchmark('Try implementation with SplFixedArray', function () use ($a, $b) {
$t = microtime(true);
merge_ordered($a, $b);
return microtime(true) - $t;
});
benchmark('Try implementation with SplFixedArray with toArray', function () use ($a, $b) {
$t = microtime(true);
merge_ordered3($a, $b);
return microtime(true) - $t;
});
benchmark('Try implementation with simple arrays', function () use ($a, $b) {
$t = microtime(true);
merge_ordered2($a, $b);
return microtime(true) - $t;
});
benchmark('Tyranron method', function () use ($a, $b) {
$t = microtime(true);
array_merge_sorted($a, $b);
return microtime(true) - $t;
});
benchmark('array_merge + sort', function () use ($a, $b) {
$t = microtime(true);
native_merge($a, $b);
return microtime(true) - $t;
});

php 5.6.2

Try implementation with SplFixedArray:
-----------------
Average execution time: 974 ms
Min execution time: 928 ms
Max execution time: 1014 ms

Try implementation with SplFixedArray with toArray:
-----------------
Average execution time: 1357 ms
Min execution time: 1338 ms
Max execution time: 1399 ms

Try implementation with simple arrays:
-----------------
Average execution time: 1358 ms
Min execution time: 1330 ms
Max execution time: 1410 ms

Tyranron method:
-----------------
Average execution time: 1339 ms
Min execution time: 1330 ms
Max execution time: 1351 ms

array_merge + sort:
-----------------
Average execution time: 1630 ms
Min execution time: 1620 ms
Max execution time: 1642 ms

hhvm 3.3

Try implementation with SplFixedArray:
-----------------
Average execution time: 621 ms
Min execution time: 600 ms
Max execution time: 641 ms

Try implementation with SplFixedArray with toArray:
-----------------
Average execution time: 624 ms
Min execution time: 584 ms
Max execution time: 656 ms

Try implementation with simple arrays:
-----------------
Average execution time: 123 ms
Min execution time: 106 ms
Max execution time: 141 ms

Tyranron method:
-----------------
Average execution time: 145 ms
Min execution time: 129 ms
Max execution time: 165 ms

array_merge + sort:
-----------------
Average execution time: 228 ms
Min execution time: 223 ms
Max execution time: 232 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment