php 处理大数组,如何在PHP中处理非常大的数组?
我有这段代码.
$input = 4;
$list_N = array('0', '1');
for($n=1; $n<=$input; $n++) {
if($n%2 == 0) {
$c++;
}
$reverse_list_N = array_reverse($list_N);
$A = array();
$B = array();
for($i=0; $i
$A[] = '0' . $list_N[$i];
$B[] = '1' . $reverse_list_N[$i];
}
$list_N = array_merge($A[], $B[]);
if($n == 1) {
$list_N = array('0', '1');
}
}
$array_sliced = array_slice($list_N, -1*$input, $input);
for($i=0; $i
$output = implode("\n", $array_sliced);
}
echo "
"; print_r($output); echo "
";
这段代码的作用是,它生成以下数据(从(0,1)开始):
0,1
00, 01, 11, 10
000, 001, 011, 010, 110, 111, 101, 100
....... and so on
当$input = 4时;输出是:
1010
1011
1001
1000
正如您所看到的,在每个循环之后,$list_N数组中的元素比前一个元素要多一倍.如果$input = 25,那么这个步伐;然后数组将有33554432个非常巨大的元素.这就是我找不到解决方案的问题.当$input = 60时,我收到此错误
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes)
在这条线上
$list_N = array_merge($A, $B);
即使将内存限制设置为2G也无法解决问题.那么,我如何优化我的代码以使内存相同.或者,还有其他解决方案吗?
更新:以下步骤用于生成数据.
$list_N is an array
$reverse_list is the reverse of $list_N
0 is appended in the beginning of every element in the $list_N array and stored in $A
1 is appended in the beginning of every element in the $reverse_list_N array and stored in $B
Array $A and array $B are merged and is stored in $list_N.
The main loop runs for $input number of times and the last $input number of elements are displayed from the final array.
最佳答案 解:
尝试使用SplFixedArray!
关于:
一个SplFixedArrayis
about 37% of a regular “array” of the same size
和
The SplFixedArray class provides the main functionalities of array. The advantage is that it allows a faster array implementation.
来自:Documentation Page
例:
$startMemory = memory_get_usage();
$array = new SplFixedArray(100000);
for ($i = 0; $i < 100000; ++$i) {
$array[$i] = $i;
}
echo memory_get_usage() - $startMemory, ' bytes';
进一步阅读:
:http://nikic.github.io/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html
梅西耶解决方案:
另一个可以帮助的解决方案,我不建议覆盖默认的内存容量.你可以这样做:
ini_set('memory_limit', '-1')
进一步阅读:
http://php.net/memory_limit
php 处理大数组,如何在PHP中处理非常大的数组?相关推荐
- python定义一个数组,如何在Python中创建一个整数数组?
It should not be so hard. I mean in C, int a[10]; is all you need. How to create an array of all zer ...
- 如何在Swift中串联或合并数组?
本文翻译自:How do I concatenate or merge arrays in Swift? If there are two arrays created in swift like t ...
- python中2d_【IT专家】如何在Python中复制一个2D数组?(复制)
本文由我司收集整编,推荐下载,如有疑问,请与我司联系 如何在 Python 中复制一个 2D 数组? ( 复制 ) 如何在 Python 中复制一个 2D 数组 ?( 复制 )[ 英 ]How to ...
- 大数据分析如何在Python中生成FiveThirtyEight图
大数据分析如何在Python中生成FiveThirtyEight图?如果你阅读大数据分析文章,则可能已经迷失了FiveThirtyEight的内容.自然,他们的出色可视化使你印象深刻.你想制作自己的出 ...
- 如何在C++中动态分配二维数组
一般是三种方法:(1)用vector的vector,(2)先分配一个指针 数组,然后让里面每一个指针再指向一个数组,这个做法的好处是访问数组元素时比较直观,可以用a[x][y]这样的写法,缺点是它相当 ...
- 如何在java中创建变长数组
传统的数组创建 在java中我们都知道创建简单数组较为简单,和C很相似.如下是创建1.2.3维数组的代码. int [] array = new int[5]; int [][] array = ne ...
- 快速提示:如何在JavaScript中创建和处理数组
这篇文章由Chris Perry和Marcello La Rocca进行了同行评审. 感谢所有SitePoint的同行评审人员使SitePoint内容达到最佳状态! Array对象的length属性是 ...
- swift java混合,如何在Swift中连接或合并数组?
使用Swift 3,根据您的需求和品味,您可以选择其中一个 five following ways 来连接/合并两个数组 . 1.使用Swift标准库(: :)泛型运算符将两个数组合并为一个新数组 S ...
- 如何在JavaScript中切片和拼接数组
.slice() and .splice() are similar methods in JavaScript. They look similar, they sound similar, and ...
最新文章
- Cache 工作原理,Cache 一致性,你想知道的都在这里
- 【转】常见浏览器兼容性问题与解决方案css篇
- 矩阵一维卷积c++_深度神经网络卷积层计算加速与优化
- 如何开发一个高性能的redis cluster proxy
- 在node.js中建立你的第一个HTTp服务器
- scp传输文件,自动填充密码
- 信息系html5论文,基于HTML5的智力游戏设计电子信息工程本科学生毕业论文.doc
- windows安装python3步骤_Windows下python3和python2安装与一起使用
- SAP License:电子行业ERP实施
- mySQL 插入,更新和删除数据
- 从单机到集群会话的管理之集群模式一
- JavaWeb文件上传(2)--使用fileupload组件
- Java零基础到进阶(真的零基础,也可以当笔记看~)
- caffe+报错︱深度学习参数调优杂记+caffe训练时的问题+dropout/batch Normalization
- 计算机分盘介质受写入保护,硬盘介质受写入保护怎么办
- 清华寒门学子「树洞文」引网友热泪:“自强不息,厚德载物”的践行者
- scsi接口服务器硬盘转速,服务器硬盘接口SCSI结构、特点详解
- 回溯算法-数独(计算器)
- android小型app创意,创意无止境!17款让人脑洞大开的APP推荐
- windows 7 下,如何统计某文件夹下 视频总时长