Created
September 17, 2021 16:59
-
-
Save MaxSem/d0ea0755d6deabaf88c9ef26039b2f27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function bench_array(int $n, int $iterations) { | |
$totalReadTime = 0.0; | |
$startTime = hrtime(true); | |
$total = 0; | |
for ($j = 0; $j < $iterations; $j++) { | |
$startMemory = memory_get_usage(); | |
$values = []; | |
for ($i = 0; $i < $n; $i++) { | |
$values[] = $i; | |
} | |
$startReadTime = hrtime(true); | |
for ($i = 0; $i < $n; $i++) { | |
$total += $values[$i]; | |
} | |
$endReadTime = hrtime(true); | |
$totalReadTime += $endReadTime - $startReadTime; | |
$endMemory = memory_get_usage(); | |
unset($values); | |
} | |
$endTime = hrtime(true); | |
$totalTime = ($endTime - $startTime) / 1000000000; | |
$totalReadTimeSeconds = $totalReadTime / 1000000000; | |
printf("Appending to array: n=%8d iterations=%8d memory=%8d bytes, create+destroy time=%.3f read time = %.3f result=%d\n", | |
$n, $iterations, $endMemory - $startMemory, $totalTime - $totalReadTimeSeconds, $totalReadTimeSeconds, $total); | |
} | |
function bench_vector(int $n, int $iterations) { | |
$startTime = hrtime(true); | |
$totalReadTime = 0.0; | |
$total = 0; | |
for ($j = 0; $j < $iterations; $j++) { | |
$startMemory = memory_get_usage(); | |
$values = new Vector(); | |
for ($i = 0; $i < $n; $i++) { | |
$values[] = $i; | |
} | |
$startReadTime = hrtime(true); | |
for ($i = 0; $i < $n; $i++) { | |
$total += $values[$i]; | |
} | |
$endReadTime = hrtime(true); | |
$totalReadTime += $endReadTime - $startReadTime; | |
$endMemory = memory_get_usage(); | |
unset($values); | |
} | |
$endTime = hrtime(true); | |
$totalTime = ($endTime - $startTime) / 1000000000; | |
$totalReadTimeSeconds = $totalReadTime / 1000000000; | |
printf("Appending to Vector: n=%8d iterations=%8d memory=%8d bytes, create+destroy time=%.3f read time = %.3f result=%d\n", | |
$n, $iterations, $endMemory - $startMemory, $totalTime - $totalReadTimeSeconds, $totalReadTimeSeconds, $total); | |
} | |
// SplStack is a subclass of SplDoublyLinkedList, so it is a linked list that takes more memory than needed. | |
// Access to values in the middle of the SplStack is also less efficient. | |
function bench_spl_stack(int $n, int $iterations) { | |
$startTime = hrtime(true); | |
$totalReadTime = 0.0; | |
$total = 0; | |
for ($j = 0; $j < $iterations; $j++) { | |
$startMemory = memory_get_usage(); | |
$values = new SplStack(); | |
for ($i = 0; $i < $n; $i++) { | |
$values->push($i); | |
} | |
$startReadTime = hrtime(true); | |
// Random access is linear time in a linked list, so use foreach instead | |
foreach ($values as $value) { | |
$total += $value; | |
} | |
$endReadTime = hrtime(true); | |
$totalReadTime += $endReadTime - $startReadTime; | |
$endMemory = memory_get_usage(); | |
unset($values); | |
} | |
$endTime = hrtime(true); | |
$totalTime = ($endTime - $startTime) / 1000000000; | |
$totalReadTimeSeconds = $totalReadTime / 1000000000; | |
printf("Appending to SplStack: n=%8d iterations=%8d memory=%8d bytes, create+destroy time=%.3f read time = %.3f result=%d\n", | |
$n, $iterations, $endMemory - $startMemory, $totalTime - $totalReadTimeSeconds, $totalReadTimeSeconds, $total); | |
} | |
function bench_spl_fixed_array(int $n, int $iterations) { | |
$startTime = hrtime(true); | |
$totalReadTime = 0.0; | |
$total = 0; | |
for ($j = 0; $j < $iterations; $j++) { | |
$startMemory = memory_get_usage(); | |
$values = new SplFixedArray(); | |
for ($i = 0; $i < $n; $i++) { | |
// Imitate how push() would be implemented in a situation | |
// where the number of elements wasn't actually known ahead of time. | |
// erealloc() tends to extend the existing array when possible. | |
$size = $values->getSize(); | |
$values->setSize($size + 1); | |
$values->offsetSet($size, $i); | |
} | |
$startReadTime = hrtime(true); | |
for ($i = 0; $i < $n; $i++) { | |
$total += $values[$i]; | |
} | |
$endReadTime = hrtime(true); | |
$totalReadTime += $endReadTime - $startReadTime; | |
$endMemory = memory_get_usage(); | |
unset($values); | |
} | |
$endTime = hrtime(true); | |
$totalTime = ($endTime - $startTime) / 1000000000; | |
$totalReadTimeSeconds = $totalReadTime / 1000000000; | |
printf("Appending to SplFixedArray: n=%8d iterations=%8d memory=%8d bytes, create+destroy time=%.3f read time = %.3f result=%d\n", | |
$n, $iterations, $endMemory - $startMemory, $totalTime - $totalReadTimeSeconds, $totalReadTimeSeconds, $total); | |
} | |
function bench_ds(int $n, int $iterations) { | |
$totalReadTime = 0.0; | |
$startTime = hrtime(true); | |
$total = 0; | |
for ($j = 0; $j < $iterations; $j++) { | |
$startMemory = memory_get_usage(); | |
$values = new Ds\Vector; | |
for ($i = 0; $i < $n; $i++) { | |
$values[] = $i; | |
} | |
$startReadTime = hrtime(true); | |
for ($i = 0; $i < $n; $i++) { | |
$total += $values[$i]; | |
} | |
$endReadTime = hrtime(true); | |
$totalReadTime += $endReadTime - $startReadTime; | |
$endMemory = memory_get_usage(); | |
unset($values); | |
} | |
$endTime = hrtime(true); | |
$totalTime = ($endTime - $startTime) / 1000000000; | |
$totalReadTimeSeconds = $totalReadTime / 1000000000; | |
printf("Appending to Ds\Vector: n=%8d iterations=%8d memory=%8d bytes, create+destroy time=%.3f read time = %.3f result=%d\n\n", | |
$n, $iterations, $endMemory - $startMemory, $totalTime - $totalReadTimeSeconds, $totalReadTimeSeconds, $total); | |
} | |
$n = 2**20; | |
$iterations = 10; | |
$sizes = [ | |
[1, 8000000], | |
[4, 2000000], | |
[8, 1000000], | |
[2**20, 20], | |
]; | |
printf( | |
"Results for php %s debug=%s with opcache enabled=%s\n\n", | |
PHP_VERSION, | |
PHP_DEBUG ? 'true' : 'false', | |
json_encode(function_exists('opcache_get_status') && (opcache_get_status(false)['opcache_enabled'] ?? false)) | |
); | |
foreach ($sizes as [$n, $iterations]) { | |
bench_array($n, $iterations); | |
bench_vector($n, $iterations); | |
bench_spl_stack($n, $iterations); | |
bench_spl_fixed_array($n, $iterations); | |
bench_ds($n, $iterations); | |
echo "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Results for php 8.2.0-dev debug=false with opcache enabled=false | |
Appending to array: n= 1 iterations= 8000000 memory= 376 bytes, create+destroy time=0.917 read time = 0.468 result=0 | |
Appending to Vector: n= 1 iterations= 8000000 memory= 128 bytes, create+destroy time=1.375 read time = 0.526 result=0 | |
Appending to SplStack: n= 1 iterations= 8000000 memory= 184 bytes, create+destroy time=2.140 read time = 1.006 result=0 | |
Appending to SplFixedArray: n= 1 iterations= 8000000 memory= 80 bytes, create+destroy time=2.302 read time = 0.578 result=0 | |
Appending to Ds\Vector: n= 1 iterations= 8000000 memory= 200 bytes, create+destroy time=1.706 read time = 0.584 result=0 | |
Appending to array: n= 4 iterations= 2000000 memory= 376 bytes, create+destroy time=0.292 read time = 0.177 result=12000000 | |
Appending to Vector: n= 4 iterations= 2000000 memory= 128 bytes, create+destroy time=0.418 read time = 0.223 result=12000000 | |
Appending to SplStack: n= 4 iterations= 2000000 memory= 280 bytes, create+destroy time=0.818 read time = 0.377 result=12000000 | |
Appending to SplFixedArray: n= 4 iterations= 2000000 memory= 128 bytes, create+destroy time=1.275 read time = 0.254 result=12000000 | |
Appending to Ds\Vector: n= 4 iterations= 2000000 memory= 200 bytes, create+destroy time=0.495 read time = 0.249 result=12000000 | |
Appending to array: n= 8 iterations= 1000000 memory= 376 bytes, create+destroy time=0.178 read time = 0.126 result=28000000 | |
Appending to Vector: n= 8 iterations= 1000000 memory= 192 bytes, create+destroy time=0.257 read time = 0.176 result=28000000 | |
Appending to SplStack: n= 8 iterations= 1000000 memory= 408 bytes, create+destroy time=0.569 read time = 0.275 result=28000000 | |
Appending to SplFixedArray: n= 8 iterations= 1000000 memory= 192 bytes, create+destroy time=1.117 read time = 0.206 result=28000000 | |
Appending to Ds\Vector: n= 8 iterations= 1000000 memory= 200 bytes, create+destroy time=0.303 read time = 0.201 result=28000000 | |
Appending to array: n= 1048576 iterations= 20 memory=33558608 bytes, create+destroy time=0.369 read time = 0.210 result=10995105792000 | |
Appending to Vector: n= 1048576 iterations= 20 memory=16777304 bytes, create+destroy time=0.270 read time = 0.270 result=10995105792000 | |
Appending to SplStack: n= 1048576 iterations= 20 memory=33554584 bytes, create+destroy time=0.893 read time = 0.397 result=10995105792000 | |
Appending to SplFixedArray: n= 1048576 iterations= 20 memory=16777304 bytes, create+destroy time=2.475 read time = 0.340 result=10995105792000 | |
Appending to Ds\Vector: n= 1048576 iterations= 20 memory=24129632 bytes, create+destroy time=0.389 read time = 0.305 result=10995105792000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment