当前位置:首页 > PHP > 正文内容

php迭代器学习

高老师8年前 (2018-07-25)PHP1959

php官方已经提供了Iterator(迭代器)接口,通过网上资料的学习,目前看适合超大集合或者数组提取使用。学习一个函数的实现对比内存占用差距.

使用迭代器和普通循环实现range()函数。

(1).普通循环实现range()函数。

function newrange($low, $hign, $step = 1)
{
    $ret = [];
    for ($i = 0; $i < $hign; $i += $step)
    {
        $ret[] = $i;
    }
    return $ret;
}

//统计内存占用
$oldMemory = memory_get_usage();
$result = newrange(0, 500000);
$newMemory = memory_get_usage();
$useMemory = $newMemory - $oldMemory;
echo 'used:' . $useMemory . 'byte';

used:14680128byte

(1).迭代器实现range()函数。

class newrange implements Iterator
{
    protected $low;
    protected $high;
    protected $step;
    protected $current;

    public function __construct($low, $high, $step = 1)
    {
        $this->low = $low;
        $this->high = $high;
        $this->step = $step;
    }

    //返回到迭代器的第一个元素
    public function rewind()
    {
        var_dump('rewind');
        $this->current = $this->low;
    }

    //向前移动到下一个元素
    public function next()
    {
        var_dump('next');
        $this->current += $this->step;
    }

    //返回当前元素
    public function current()
    {
        var_dump('current');
        return $this->current;
    }

    //返回当前元素的键
    public function key()
    {
        var_dump('key');
        return $this->current + 1;
    }

    //检查当前位置是否有效
    public function valid()
    {
        var_dump('valid');
        return $this->current <= $this->high;
    }
}

//统计内存占用
$oldMemory = memory_get_usage();
$result = new newrange(0, 9, 1);
/*
foreach ($result as $key => $value)
{

}
*/
$newMemory = memory_get_usage();
$useMemory = $newMemory - $oldMemory;
echo 'used:' . $useMemory . 'byte';

used:96byte

再来使用php自带的pdo实现的迭代器测试大数据占用内存.

(1).数据是我们公司正式服务器拉取下拉的,保证数据不是随便生成的,之前在迁移数据使用mysqli提取单表53w数组内存超过限制(每条记录字段50个)。

(2).本次改用pdo的query方法,因为query内部用php迭代器实现

//起始内存
$oldMemory = memory_get_usage();

$host = '127.0.0.1:9944';
$datebase = 'dev_dnet1';
$username = 'dev_dnet2';
$password = 'dev_dnet3';
$conn = "mysql:host=$host;dbname=$datebase";

$db = new PDO($conn, $username, $password);
$sql = 'select *  from prd_product';
$result = $db->query($sql);

//结束内存
$newMemory = memory_get_usage();
$useMemory = $newMemory - $oldMemory;
echo 'used:' . $useMemory . 'byte';

used:1563928408byte  1491M

总结:

    (1).迭代器仅仅只是产生1个对象的内存,因此在使用超大数组的时候可以选择迭代器,只不过使用起来麻烦点,但是至少比使用数组性能好多了。

    (2).如果想要看到内部执行过程只需要使用foreach直接循环结果集,php内部帮我们执行了对应方法来实现迭代

    (3).PHP数组的内存利用率只有 1/10,但是我们可以用迭代器啊,世界上最好的编程语言

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/65.html

分享给朋友:

“php迭代器学习” 的相关文章

php 经纬度获取城市,php 经纬度转城市

php 经纬度获取城市,php 经纬度转城市

通过经纬度转换为城市名称,并获得城市的编号,通常在全国类型的商城中比较通用。//经纬度转城市名称(返回城市id,城市名称) //$ak开发密钥,$lat纬度,$lng经度,$type返回数据类型 public function getCityName($ak,$lat,$lng)...

php soap 捕获异常,使用try catch 捕获Soap 异常

php soap 捕获异常,使用try catch 捕获Soap 异常

项目中使用服务来执行webservice,由于对方系统api不稳定,经常导致服务崩溃,只能重启,一个月差不多要重启一次。初期的解决办法是捕获异常,然后continue掉。<?php try {     $url = 'http...

php json_encode 使用注意

php json_encode 使用注意

参数中包含gb2312的字符串,返回结果是false或者null(不同PHP版本具有差异性)代码:<?php $dbms = 'mysql'; $host = '192.168.8.8'; $dbName =&n...

pcntl_signal(): Error assigning signal

pcntl_signal(): Error assigning signal

当我想在一个进程中监听kill 和 kill -9命令报了这个错误。//监听kill pcntl_signal(SIGTERM, function () {     posix_kill(0, SIGTERM); });...

php生成器的send方法详解,php yield send

php生成器的send方法详解,php yield send

【一】.基本用法首先看看官方对send方法的解释:Sets the return value of the yield expression and resumes the generato...

 thinkphp5定时任务,tp5定时任务,thinkphp定时任务,php定时任务,php定时器

thinkphp5定时任务,tp5定时任务,thinkphp定时任务,php定时任务,php定时器

本教程使用的定时任务基于EasyTak,EasyTask官方文档:https://gitee.com/392223903/EasyTask(1).安装tp5.0或者5.1composer create-project topthink/think=5.0.* tp5&n...