Last active
June 26, 2025 10:55
-
-
Save KVytyagov/57509333daaac69eb980777c7d28ea18 to your computer and use it in GitHub Desktop.
performance instanceof vs eq(null)
This file contains hidden or 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 | |
$iterations = 10**6; | |
class SomeVeryLongNameForCompareWithSomeVeryPerfectMethodOrStrategyAndSomeOtherWordsInThisName {} | |
class Short{} | |
function checkInstanceOfLongClassName($val): bool { | |
return $val instanceof SomeVeryLongNameForCompareWithSomeVeryPerfectMethodOrStrategyAndSomeOtherWordsInThisName; | |
} | |
function checkInstanceOfShortClassName($val): bool { | |
return $val instanceof Short; | |
} | |
function nullEquality($val): bool { | |
return $val === null; | |
} | |
$testFunctions = [ | |
'checkInstanceOfLongClassName', | |
'checkInstanceOfShortClassName', | |
'nullEquality', | |
]; | |
$checkedValues = [ | |
'null' => null, | |
'shortClassName' => new Short(), | |
'longClassName' => new SomeVeryLongNameForCompareWithSomeVeryPerfectMethodOrStrategyAndSomeOtherWordsInThisName(), | |
]; | |
foreach ($testFunctions as $functionName) { | |
foreach ($checkedValues as $valueName => $value) { | |
$results = []; | |
for($i = 0; $i < $iterations; ++$i) { | |
$start = microtime(true); | |
$memoryPeakOnStart = memory_get_peak_usage(true); | |
$res = $functionName($value); | |
$time = microtime(true) - $start; | |
$memoryPeakIncrease = memory_get_peak_usage(true) - $memoryPeakOnStart; | |
$results['times'][] = $time; | |
$results['memoryPeak'][] = $memoryPeakIncrease; | |
} | |
echo sprintf( | |
"Method '%s' for '%s' \n\tavgTime: %f\n\tavgMemoryPeak: %d\n\tminTime: %d\n\tmaxTime: %d\n", | |
$functionName, | |
$valueName, | |
array_sum($results['times']) / count($results['times']), | |
array_sum($results['memoryPeak']) / count($results['memoryPeak']), | |
min($results['times']), | |
max($results['times']) | |
); | |
} | |
} |
PHP 8.2.13 (cli) (built: Nov 24 2023 13:10:01) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.13, Copyright (c) Zend Technologies
with Zend OPcache v8.2.13, Copyright (c), by Zend Technologies
Method 'checkInstanceOfLongClassName' for 'null'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
Method 'checkInstanceOfLongClassName' for 'shortClassName'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
Method 'checkInstanceOfLongClassName' for 'longClassName'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
Method 'checkInstanceOfShortClassName' for 'null'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
Method 'checkInstanceOfShortClassName' for 'shortClassName'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
Method 'checkInstanceOfShortClassName' for 'longClassName'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
Method 'nullEquality' for 'null'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
Method 'nullEquality' for 'shortClassName'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
Method 'nullEquality' for 'longClassName'
avgTime: 0.000000
avgMemoryPeak: 0
minTime: 0
maxTime: 0
<?php
$iterations = 10**6;
class SomeVeryLongNameForCompareWithSomeVeryPerfectMethodOrStrategyAndSomeOtherWordsInThisName {}
class Short {}
function checkInstanceOfLongClassName($val): bool {
return $val instanceof SomeVeryLongNameForCompareWithSomeVeryPerfectMethodOrStrategyAndSomeOtherWordsInThisName;
}
function checkInstanceOfShortClassName($val): bool {
return $val instanceof Short;
}
function nullEquality($val): bool {
return $val === null;
}
$testFunctions = [
'checkInstanceOfLongClassName',
'checkInstanceOfShortClassName',
'nullEquality',
];
$checkedValues = [
'null' => null,
'shortClassName' => new Short(),
'longClassName' => new SomeVeryLongNameForCompareWithSomeVeryPerfectMethodOrStrategyAndSomeOtherWordsInThisName(),
];
foreach ($testFunctions as $functionName) {
foreach ($checkedValues as $valueName => $value) {
$totalTime = 0;
$minTime = PHP_FLOAT_MAX;
$maxTime = PHP_FLOAT_MIN;
for ($i = 0; $i < $iterations; ++$i) {
$start = microtime(true);
$functionName($value);
$time = microtime(true) - $start;
$totalTime += $time;
if ($time < $minTime) $minTime = $time;
if ($time > $maxTime) $maxTime = $time;
}
printf(
"Method '%s' for '%s' \n\tavgTime: %e\n\tminTime: %e\n\tmaxTime: %e\n",
$functionName,
$valueName,
$totalTime / $iterations,
$minTime,
$maxTime
);
}
}
?>
php -v
PHP 8.1.29 (cli) (built: Jun 6 2024 16:31:57) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.29, Copyright (c) Zend Technologies
with Zend OPcache v8.1.29, Copyright (c), by Zend Technologies
with Xdebug v3.3.2, Copyright (c) 2002-2024, by Derick Rethans
output:
Method 'checkInstanceOfLongClassName' for 'null'
avgTime: 5.298059e-7
minTime: 0.000000e+0
maxTime: 1.225948e-3
Method 'checkInstanceOfLongClassName' for 'shortClassName'
avgTime: 4.514792e-7
minTime: 0.000000e+0
maxTime: 2.098083e-5
Method 'checkInstanceOfLongClassName' for 'longClassName'
avgTime: 4.443722e-7
minTime: 0.000000e+0
maxTime: 2.503395e-5
Method 'checkInstanceOfShortClassName' for 'null'
avgTime: 4.409249e-7
minTime: 0.000000e+0
maxTime: 2.694130e-5
Method 'checkInstanceOfShortClassName' for 'shortClassName'
avgTime: 4.441946e-7
minTime: 0.000000e+0
maxTime: 2.598763e-5
Method 'checkInstanceOfShortClassName' for 'longClassName'
avgTime: 4.640095e-7
minTime: 0.000000e+0
maxTime: 1.001358e-4
Method 'nullEquality' for 'null'
avgTime: 4.333978e-7
minTime: 0.000000e+0
maxTime: 3.314018e-5
Method 'nullEquality' for 'shortClassName'
avgTime: 4.192696e-7
minTime: 0.000000e+0
maxTime: 6.008148e-5
Method 'nullEquality' for 'longClassName'
avgTime: 4.119089e-7
minTime: 0.000000e+0
maxTime: 3.504753e-5
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.