Skip to content

Instantly share code, notes, and snippets.

@castarco
Created December 16, 2016 14:37
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 castarco/0987d06b2b3baf55903be09b34d9bb0a to your computer and use it in GitHub Desktop.
Save castarco/0987d06b2b3baf55903be09b34d9bb0a to your computer and use it in GitHub Desktop.
Simple benchmark for RW operations on Ds\Vector (PHP)
<?php
declare(strict_types=1);
/**
* Requires the DS extension (see https://github.com/php-ds/extension).
* This script is a benchmark for simple RW operations on Ds\Vector (without affecting its length)
*/
/**
* RESULTS (the times are not divided by the number of iterations, only numbers in the same row can be compared):
*
* size r_a r_v w_a w_v rw_a rw_w
* 00008 -> 0.000354 0.000474 0.000337 0.000451 0.000466 0.000618
* 00016 -> 0.000630 0.000859 0.000599 0.000822 0.000871 0.001165
* 00032 -> 0.001120 0.001423 0.001066 0.001491 0.001555 0.002214
* 00064 -> 0.002123 0.002596 0.001962 0.002764 0.002891 0.003933
* 00096 -> 0.002947 0.003693 0.002767 0.003804 0.003812 0.005167
* 00128 -> 0.003540 0.004408 0.003353 0.004607 0.004791 0.006475
* 00192 -> 0.004841 0.005969 0.004420 0.006269 0.006346 0.008636
* 00256 -> 0.005749 0.007007 0.005291 0.007329 0.007597 0.010143
* 00384 -> 0.007592 0.009244 0.006945 0.009612 0.009866 0.013267
* 00512 -> 0.008821 0.010696 0.008021 0.011450 0.012146 0.016743
* 00768 -> 0.012708 0.015731 0.012118 0.017123 0.018058 0.024873
* 01024 -> 0.016975 0.020951 0.016109 0.022855 0.024011 0.033165
* 02048 -> 0.033802 0.041820 0.032195 0.045930 0.048067 0.066219
* 04096 -> 0.067623 0.083642 0.064445 0.091441 0.096156 0.133349
* 08192 -> 0.135457 0.167361 0.129221 0.182602 0.192323 0.265711
* 16536 -> 0.273320 0.337747 0.259966 0.369837 0.388591 0.535953
*
*/
require __DIR__ . '/../vendor/autoload.php';
use Ds\Vector;
$vecSizes = [8, 16, 32, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 2048, 4096, 8192, 16536];
if (\extension_loaded('ds')) {
echo "\n\nLOADED DS\n\n";
} else {
echo "\n\nNO EXTENSION\n\n";
}
$result = [];
foreach ($vecSizes as $nElements) {
$a = \array_fill(0, $nElements, 42);
$v = new Vector($a);
$r_a_t1 = microtime(true);
for ($x = 0; $x < 1000; $x++) {
for ($i = 0; $i < $nElements; $i++) {
$z = $a[$i];
}
}
$r_a_t2 = microtime(true);
$r_v_t1 = microtime(true);
for ($x = 0; $x < 1000; $x++) {
for ($i = 0; $i < $nElements; $i++) {
$z = $v[$i];
}
}
$r_v_t2 = microtime(true);
$z = 11;
$w_a_t1 = microtime(true);
for ($x = 0; $x < 1000; $x++) {
for ($i = 0; $i < $nElements; $i++) {
$a[$i] = $z;
}
}
$w_a_t2 = microtime(true);
$w_v_t1 = microtime(true);
for ($x = 0; $x < 1000; $x++) {
for ($i = 0; $i < $nElements; $i++) {
$v[$i] = $z;
}
}
$w_v_t2 = microtime(true);
$rw_a_t1 = microtime(true);
for ($x = 0; $x < 1000; $x++) {
for ($i = 0; $i < $nElements; $i++) {
$a[$i] += $z;
}
}
$rw_a_t2 = microtime(true);
$rw_v_t1 = microtime(true);
for ($x = 0; $x < 1000; $x++) {
for ($i = 0; $i < $nElements; $i++) {
$v[$i] += $z;
}
}
$rw_v_t2 = microtime(true);
$result[] = [
'n' => $nElements,
'r_a' => $r_a_t2 - $r_a_t1,
'r_v' => $r_v_t2 - $r_v_t1,
'w_a' => $w_a_t2 - $w_a_t1,
'w_v' => $w_v_t2 - $w_v_t1,
'rw_a' => $rw_a_t2 - $rw_a_t1,
'rw_v' => $rw_v_t2 - $rw_v_t1,
];
}
echo " r_a r_v w_a w_v rw_a rw_w \n";
foreach ($result as $row) {
printf(
"%05d -> %08.f %08.f %08.f %08.f %08.f %08.f\n",
$row['n'], $row['r_a'], $row['r_v'], $row['w_a'], $row['w_v'], $row['rw_a'], $row['rw_v']
);
}
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment