PHP数组交集的优化代码分析
时间:2023-09-29 21:58:59
不过由于手机的参数多,且不同的手机其参数差异大,所以参数表结构通常是纵表(一个参数是一行),而不是横表(一个参数是一列),此时使用若干参数来取结果,通常就是把每个单独参数来取结果,再一起取交集。
假定每个参数会包含一千个左右的唯一结果(id int),以此为前提来模拟生成一些数据:
<?php
$rand = function() {
$result = array();
for ($i = 0; $i < 1000; null) {
$value = mt_rand(1, 10000);
if (!isset($result[$value])) {
$result[$value] = null;
$i++;
}
}
return array_keys($result);
};
$param_a = $rand();
$param_b = $rand();
?>
注意:如果测试数据集过小的话,结论可能会出现不一致,先来看看通过PHP内置方法array_intersect实现的性能:
<?php
$time = microtime(true);
$result = array_intersect($param_a, $param_b);
$time = microtime(true) - $time;
echo "array_intersect: {$time}\n";
?>
再来看看通过自定义方法intersect实现的性能:
<?php
function intersect() {
if (func_num_args() < 2) {
trigger_error('param error', E_USER_ERROR);
}
$args = func_get_args();
foreach ($args AS $arg) {
if (!is_array($arg)) {
trigger_error('param error', E_USER_ERROR);
}
}
$intersect = function($a, $b) {
$result = array();
$length_a = count($a);
$length_b = count($b);
for ($i = 0, $j = 0; $i < $length_a && $j < $length_b; null) {
if($a[$i] < $b[$j]) {
$i++;
} else if($a[$i] > $b[$j]) {
$j++;
} else {
$result[] = $a[$i];
$i++;
$j++;
}
}
return $result;
};
$result = array_shift($args);
sort($result);
foreach ($args as $arg) {
sort($arg);
$result = $intersect($result, $arg);
}
return $result;
}
$time = microtime(true);
$result = intersect($param_a, $param_b);
$time = microtime(true) - $time;
echo "intersect: {$time}\n";
?>
直觉上,我们肯定会认为内置函数快于自定义函数,但本例中结果恰恰相反:
array_intersect: 0.023918151855469
intersect: 0.0026049613952637
需要提醒大家的是,array_intersect和intersect在功能上并不完全等价,例子如下:
$param_a = array(1, 2, 2);
$param_b = array(1, 2, 3);
var_dump(
array_intersect($param_a, $param_b),
intersect($param_a, $param_b)
);
array_intersect: 1, 2, 2
intersect: 1, 2
也就是说,如果在第一个数组参数中有重复元素的话,则array_intersect会返回所有满足条件的重复元素,而不是仅仅返回一个,有兴趣的读者可以变换一下参数顺序再看结果。
再唠叨一下,最初我写intersect方法时,大概写成下面这个样子:
<?php
function intersect() {
if (func_num_args() < 2) {
trigger_error('param error', E_USER_ERROR);
}
$args = func_get_args();
foreach ($args AS $arg) {
if (!is_array($arg)) {
trigger_error('param error', E_USER_ERROR);
}
}
$result = array();
$data = array_count_values(
call_user_func_array('array_merge', $args)
);
foreach ($data AS $value => $count) {
if ($count > 1) {
$result[] = $value;
}
}
return $result;
}
?>
代码更简洁,不过有一个弊端,因为使用了array_merge,所以当数组中元素非常多的时候,占用的内存会比较大,反之如果数组中元素不是非常多,那么此方法也是可行的。(来源:火丁笔记)
标签:PHP,数组交集
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中三种输出格式总结(%,format,f-string)
2022-06-11 16:34:40
![](https://img.aspxhome.com/file/2023/6/75356_0s.png)
python中函数返回多个结果的实例方法
2023-02-27 14:46:50
django之状态保持-使用redis存储session的例子
2023-07-29 20:14:07
python的ImageTk.PhotoImage大坑及解决
2021-02-24 13:53:04
python实现凯撒密码、凯撒加解密算法
2023-08-27 17:49:22
python使用pyodbc连接sqlserver
2021-12-24 06:06:17
![](https://img.aspxhome.com/file/2023/1/69031_0s.png)
python实现邮箱发送信息
2023-02-25 15:11:51
Pytorch实现Fashion-mnist分类任务全过程
2023-07-14 05:12:47
![](https://img.aspxhome.com/file/2023/3/62323_0s.png)
详解如何在nuxt中添加proxyTable代理
2024-05-10 14:20:29
Oracle密码错误次数过多账号锁定的问题(推荐)
2024-01-14 13:24:05
超详细OpenMV与STM32单片机通信 附完整源码
2023-05-27 17:03:50
![](https://img.aspxhome.com/file/2023/7/134727_0s.jpg)
[JS]用 或 || 来兼容FireFox
2013-06-26 14:50:47
CentOS6.9+Mysql5.7.18源码安装详细教程
2024-01-19 02:42:14
MySql数据库中Select用法小结
2024-01-22 08:45:42
初识Firebug 全文 — firebug的使用
2007-10-23 12:54:00
![](https://img.aspxhome.com/file/UploadPic/200710/23/2007102313756216s.gif)
js 删除数组的几种方法小结
2024-04-22 22:42:37
python MultipartEncoder传输zip文件实例
2022-04-20 07:22:57
Gradio机器学习模型快速部署工具quickstart前篇
2023-07-01 15:07:51
![](https://img.aspxhome.com/file/2023/4/62844_0s.png)
Python基于OpenCV的视频图像处理详解
2021-02-06 19:57:37
![](https://img.aspxhome.com/file/2023/5/109485_0s.jpg)
使用python实现定时报天气的示例代码
2021-12-27 14:56:10
![](https://img.aspxhome.com/file/2023/9/105199_0s.png)