Skip to content

Instantly share code, notes, and snippets.

@KVytyagov
Last active June 26, 2025 10:55
Show Gist options
  • Save KVytyagov/57509333daaac69eb980777c7d28ea18 to your computer and use it in GitHub Desktop.
Save KVytyagov/57509333daaac69eb980777c7d28ea18 to your computer and use it in GitHub Desktop.
performance instanceof vs eq(null)
<?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'])
);
}
}
@KVytyagov
Copy link
Author

KVytyagov commented Nov 19, 2019

PHP 7.2.24-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 24 2019 18:29:11) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
Method 'checkInstanceOfLongClassName' for 'null' 
	avgTime: 0.000004
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0
Method 'checkInstanceOfLongClassName' for 'shortClassName' 
	avgTime: 0.000003
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0
Method 'checkInstanceOfLongClassName' for 'longClassName' 
	avgTime: 0.000003
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0
Method 'checkInstanceOfShortClassName' for 'null' 
	avgTime: 0.000004
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0
Method 'checkInstanceOfShortClassName' for 'shortClassName' 
	avgTime: 0.000004
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0
Method 'checkInstanceOfShortClassName' for 'longClassName' 
	avgTime: 0.000003
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0
Method 'nullEquality' for 'null' 
	avgTime: 0.000002
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0
Method 'nullEquality' for 'shortClassName' 
	avgTime: 0.000002
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0
Method 'nullEquality' for 'longClassName' 
	avgTime: 0.000003
	avgMemoryPeak: 0
	minTime: 0
	maxTime: 0

Copy link

ghost commented Dec 13, 2023

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

Copy link

ghost commented Sep 3, 2024

<?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