|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80348 10.000% inneficiency when using array_intersect_key and array_intersect_ukey
Submitted: 2020-11-10 15:00 UTC Modified: 2020-11-11 19:58 UTC
From: fuckoff at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.12 OS: All
Private report: No CVE-ID: None
 [2020-11-10 15:00 UTC] fuckoff at gmail dot com
Please run the test script. In my computer it outputs this:

 1-> 0.0002131462097168
 2-> 0.02832914352417

It's a 10.000% difference (100 * 0.0002 / 0.02) when using 2 supposedly very similar functions, with only a small difference that one allows a callback to be used.

I really need to use `array_intersect_ukey` but the performance hit is huge. I tried creating my own PHP implementation of `array_intersect_ukey` and it runs a lot faster, but still is much slower than `array_intersect_key` and I really need something similar to `array_intersect_key` in terms of speed (it can even be 10x slower, but not 100x or worse.

Test script:

$a = array();
$b = array();

for ($i=0;$i<1000;$i++) {

	$a[$i] = true;
	$b[2*$i + 1] = true;


$z = microtime(true);

for ($i=0;$i<10;$i++) {
	$temp = array_intersect_key($a,$b);

echo "\n 1-> " . (microtime(true) - $z);

$z = microtime(true);

for ($i=0;$i<10;$i++) {
	$temp = array_intersect_ukey($a,$b,function($key1,$key2) {
		if ($key1 === $key2) {

			return 0;

			return $key2 - $key1;

echo "\n 2-> " . (microtime(true) - $z);


Expected result:
Both functions should run at similar speeds.

Actual result:
One function runs 10.000% slower than the other.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-10 15:57 UTC]
-Status: Open +Status: Not a bug
 [2020-11-10 15:57 UTC]
"using 2 supposedly very similar functions"

They perform similar tasks, they don't behave the same internally. One of them needs to call userland code from internal code. The other one doesn't.

Reporting non-existant performance bugs is not useful. It takes people time away from fixing actual bugs, or analysing where performance problems are across the whole of the project. Please just get a faster server, rather than expecting other people to magically make your particular use-case faster.
 [2020-11-10 16:04 UTC]
Algorithmically, array_intersect_key and array_intersect_ukey have pretty much no relation. array_intersect_key is a hash-based intersection, array_intersect_ukey is a comparison-based intersection.

To implement an efficient intersection in userland, you need to approach it from the hash table angle, as comparison-oriented intersections are fundamentally slow.
 [2020-11-10 17:39 UTC] gilperon at gmail dot com I didnt say anywhere in my text/description that this was a bug, I just said it's amazing how poorly one of the functions does while doing almost the same exactly thing as the other (intersecting keys). If you think this is normal and expected, and if you think this is a pretty common thing that most users/programmers will know (that one function is terribly slow compared to the other) than I am just an idiot wasting your time. You are welcome.
 [2020-11-11 19:29 UTC] a at b dot c dot de
You posted in a bug tracker.
 [2020-11-11 19:57 UTC] gilperon at gmail dot com
A bug tracker is not a place where only are reported bugs which are 100% a bug. It's a tracker, people can check/discuss whether it's a bug or not.
 [2020-11-11 19:58 UTC] fuckoff at gmail dot com
-: gilperon at gmail dot com +: fuckoff at gmail dot com
 [2020-11-11 19:58 UTC] fuckoff at gmail dot com
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Jul 06 01:05:44 2022 UTC