Skip to content

Instantly share code, notes, and snippets.

<?php
$intervalsCount = 100;
$days = range(0, 100);
//shuffle($days);
$iteratedDays = array_slice($days, 0, $intervalsCount);
$intervals = [];
foreach ($iteratedDays as $days) {
@KVytyagov
KVytyagov / find_result_check_performance.php
Last active June 26, 2025 10:55
performance instanceof vs eq(null)
<?php
$iterations = 10**6;
class SomeVeryLongNameForCompareWithSomeVeryPerfectMethodOrStrategyAndSomeOtherWordsInThisName {}
class Short{}
function checkInstanceOfLongClassName($val): bool {
return $val instanceof SomeVeryLongNameForCompareWithSomeVeryPerfectMethodOrStrategyAndSomeOtherWordsInThisName;
}
@KVytyagov
KVytyagov / concat-performance-comparison
Created October 22, 2019 07:28
concat string comparison
<?php
$iterations = 10**7;
$part1 = 'some_part1_string';
$part2 = 'some_part2_string';
$part3 = 'some_part3_string';
$part4 = 'some_part4_string';
function singleQuotesConcat(string $p1, string $p2, string $p3, string $p4): string {
return $p1 . '-' . $p2 . '-' . $p3 . '-' . $p4;
@KVytyagov
KVytyagov / array_unique_values_performance.php
Last active October 9, 2019 10:45
Check performance to get unique values from array
<?php
$iterations = 10;
$originValuesTo = 10000;
$duplicateValuesTo = 10000;
$duplicatesCount = 2;
$a = range(0, $originValuesTo);
$b = range(0, $duplicateValuesTo);
@KVytyagov
KVytyagov / big-int-sum.php
Created October 10, 2018 18:27
Function to sum two integers in strings that bigger then PHP_INT_MAX
<?php
declare(strict_types=1);
function getLastIdx(string $strInt): int {
return '' === $strInt ? -1 : strlen($strInt) - 1;
}
function getDigitByIdx(string $strInt, int $idx): int {
return $idx >= 0 ? (int) $strInt[$idx] : 0;
@KVytyagov
KVytyagov / array_merge-vs-array_push-in-loop.php
Created May 11, 2018 13:56
Performance test array_merge vs array_push in loop
<?php
declare(strict_types=1);
$arraysCount = 100;
$itemsInArray = 100;
$dataToMerge = [];
for ($i = 0; $i < $arraysCount; ++$i) {
for ($j = 0; $j < $itemsInArray; ++$j) {
$dataToMerge[$i][] = uniqid('', true);
}
@KVytyagov
KVytyagov / strtr-vs-str_replace.php
Last active July 11, 2024 11:47
Performance test strtr vs str_replace
<?php
$iterations = 10000000;
$defaultString = 'Test string m8';
$replaceTpl = 'm8';
$placeholder = 'is more effective';
$start = microtime(true);
for ($i = 0; $i < $iterations; ++$i) {
$out = str_replace($replaceTpl, $placeholder, $defaultString);
@KVytyagov
KVytyagov / isset-vs-array_key_exists-vs-both.php
Last active March 13, 2020 10:38
Test performance of isset, array_key_exists, isset OR array_key_exists on NULL-array item
<?php
$a = null;
$key = null;
$startIdx = 0;
$func = function() use (&$a, &$key, &$startIdx) {
$a = array_fill_keys(range($startIdx, $startIdx+=1000), null);
$key = $startIdx + 1;
};
@KVytyagov
KVytyagov / to-array-conversion-perf-test.php
Last active April 11, 2018 13:03
Test for performance conversion variable to array with checking it's type and without it
<?php
class TestResult
{
/**
* @var int
*/
private $count;
/**
* @var float