SplFixedArray主要是处理数组相关的主要功能,与普通php array不同的是,它是固定长度的,且以数字为键名的数组,优势就是比普通的数组处理更快。

看看我本机的Benchmark测试:

ini_set('memory_limit','12800M');

for($size = 10000; $size < 10000000; $size *= 4) {
echo PHP_EOL . "Testing size: $size" . PHP_EOL;
for($s = microtime(true), $container = Array(), $i = 0; $i < $size; $i++) $container[$i] = NULL;
echo "Array(): " . (microtime(true) - $s) . PHP_EOL; for($s = microtime(true), $container = new SplFixedArray($size), $i = 0; $i < $size; $i++) $container[$i] = NULL;
echo "SplArray(): " . (microtime(true) - $s) . PHP_EOL;
}

结果如下:

Testing size: 10000
Array(): 0.004000186920166
SplArray(): 0.0019998550415039 Testing size: 40000
Array(): 0.017001152038574
SplArray(): 0.0090007781982422 Testing size: 160000
Array(): 0.050002098083496
SplArray(): 0.046003103256226 Testing size: 640000
Array(): 0.19701099395752
SplArray(): 0.16700983047485 Testing size: 2560000
Array(): 0.75704312324524
SplArray(): 0.67303895950317

通常情况下SplFixedArray要比php array快上20%~30%,所以如果你是处理巨大数量的固定长度数组,还是强烈建议使用。
SplFixedArray类摘要如下:

SplFixedArray implements Iterator  , ArrayAccess  , Countable  {
/* 方法 */
public __construct ([ int $size = 0 ] )
public int count ( void )
public mixed current ( void )
public static SplFixedArray fromArray ( array $array [, bool $save_indexes = true ] )
public int getSize ( void )
public int key ( void )
public void next ( void )
public bool offsetExists ( int $index )
public mixed offsetGet ( int $index )
public void offsetSet ( int $index , mixed $newval )
public void offsetUnset ( int $index )
public void rewind ( void )
public int setSize ( int $size )
public array toArray ( void )
public bool valid ( void )
public void __wakeup ( void )
}

使用SplFixedArray:

$arr = new SplFixedArray(4);
$arr[0] = 'php';
$arr[1] = 1;
$arr[3] = 'python'; //遍历, $arr[2] 为null
foreach($arr as $v) {
echo $v . PHP_EOL;
} //获取数组长度
echo $arr->getSize(); //4 //增加数组长度
$arr->setSize(5);
$arr[4] = 'new one'; //捕获异常
try{
echo $arr[10];
} catch (RuntimeException $e) {
echo $e->getMessage();
}

PHP SPL标准库之SplFixedArray使用实例的更多相关文章

  1. php spl标准库简介(SPL是Standard PHP Library(PHP标准库)(直接看代码实例,特别方便)

    php spl标准库简介(SPL是Standard PHP Library(PHP标准库)(直接看代码实例,特别方便) 一.总结 直接看代码实例,特别方便易懂 thinkphp控制器利眠宁不支持(说明 ...

  2. PHP的SPL标准库里面的堆(SplHeap)怎么使用

    PHP的SPL标准库里面的堆(SplHeap)怎么使用 一.总结 1.因为SplHeap是抽象类,所以要先继承,实现里面的抽象方法compare后,才能new对象使用. 二.PHP的SPL标准库里面的 ...

  3. PHP SPL标准库之数据结构栈(SplStack)介绍(基础array已经可以解决很多问题了,现在开始解决问题)

    PHP SPL标准库之数据结构栈(SplStack)介绍(基础array已经可以解决很多问题了,现在开始解决问题) 一.总结 SplStack就是继承双链表(SplDoublyLinkedList)实 ...

  4. PHP SPL标准库-接口

    PHP SPL标准库有一下接口: Countable OuterIterator RecursiveIterator SeekableIterator SplObserver SplSubject A ...

  5. PHP 设计模式 笔记与总结(3)SPL 标准库

    SPL 库的使用(PHP 标准库) 1. SplStack,SplQueue,SplHeap,SplFixedArray 等数据结构类 ① 栈(SplStack)(先进后出的数据结构) index.p ...

  6. 【SPL标准库专题(1)】 SPL简介

    什么是SPL SPL是Standard PHP Library(PHP标准库)的缩写. 根据官方定义,它是"a collection of interfaces and classes th ...

  7. 【SPL标准库专题(8)】 Datastructures:SplFixedArray

    SplFixedArray主要是处理数组相关的主要功能,与普通php array不同的是,它是固定长度的,且以数字为键名的数组,优势就是比普通的数组处理更快. 类摘要 SplFixedArray im ...

  8. PHP的SPL标准库

    1,简介 SPL,全称 Standard PHP Library 中文是 标准PHP类库.是php内置的一些拓展类和拓展接口,其内容包含数据结构.迭代器.接口.异常.SPL函数,文件处理等内容.SPL ...

  9. 【SPL标准库专题(3)】 Classes

    我把SPL分为四个部分:Iterator,Classes,Datastructures,Function:而其中classes是就是做一些类的介绍(Iterator与Datastructures相关的 ...

随机推荐

  1. YTU 2989: 顺序表基本运算(线性表)

    2989: 顺序表基本运算(线性表) 时间限制: 1 Sec  内存限制: 128 MB 提交: 1  解决: 1 题目描述 编写一个程序,实现顺序表的各种基本运算(假设顺序表的元素类型为char), ...

  2. c# 之抽象工厂模式

    Email整体项目 Email类 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  3. c# UDP通信

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. 2016年11月1日 星期二 --出埃及记 Exodus 19:17

    2016年11月1日 星期二 --出埃及记 Exodus 19:17 Then Moses led the people out of the camp to meet with God, and t ...

  5. sql重新排序

    declare @i int select @i = 10 update dbo.T_StartEndCode set @i = @i+1,OrderNumber = @i

  6. 各操作系统配置java环境变量

    Windows 1. JAVA_HOME -->>  E:\java-tools\Java\JDK8_64\jdk1.8.0_77 2. path -->> %JAVA_HOM ...

  7. 函数式编程之block

    语法: 注意: 1,在代码块中可以使用和改变全局变量 2,而局部变量可以使用,但是不能改变. 怎么在代码块中改变局部变量呢?在局部变量前面加上关键字:__block 参考: Objective-C语法 ...

  8. mysql 修改数据库的列

    alter table tableName oldcolumn newcolumn datatype;

  9. ubuntu下phpstorm无法输入中文的解决办法

    http://blog.csdn.net/woshiliulei0/article/details/51657356 今天期待已久的搜狗输入法linux版上线了,对于我们这种之前用习惯了搜狗输入法的屌 ...

  10. Nessus基本命令

    /etc/init.d/nessusd start 启动nessusd服务 默认端口8834 添加用户(未知是什么用户) sudo /opt/nessus/sbin/nessus-adduser