美文网首页程序员
PHP SPL标准库之固定大小数组

PHP SPL标准库之固定大小数组

作者: Anomaly | 来源:发表于2017-07-21 14:57 被阅读0次

简介

SplFixedArray,字面意思就是固定大小的数组,意思就是在定义的时候就要确定大小,有点C语言基础的人估计都知道在C语言里面数组的大小是固定的,而PHP则随意很多,代价就是性能,所以这个数组比那个性能高很多,这里有一组数据:

On a PHP 5.4 64 bits linux server, I found SplFixedArray to be always faster than array().

  • small data (1,000):
    • write: SplFixedArray is 15 % faster
    • read: SplFixedArray is 5 % faster
  • larger data (512,000):
    • write: SplFixedArray is 33 % faster
    • read: SplFixedArray is 10 % faster

1.类摘要

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 )
}

2.实例

<?php

namespace SPL;

$fixArr    = new \SplFixedArray(10);
$fixArr[0] = 1;
$fixArr[1] = 2;
$fixArr[2] = 3;

foreach ($fixArr as $value) {
    echo $value . "\n";
}

从使用上说,fixed 数组和普通数组大部分没有区别,最大的问题就在于你必须先确定数组大小!某些情况下还是挺有用,能够提升性能!

相关文章

  • PHP SPL标准库之固定大小数组

    简介 SplFixedArray,字面意思就是固定大小的数组,意思就是在定义的时候就要确定大小,有点C语言基础的人...

  • PHP面试题

    1,PHP SPL(PHP标准库) SPL是用于解决典型问题(standard problems)的一组接口与类的...

  • PHP SPL标准库之堆

    简介 堆(英语:heap)是计算机科学中一类特殊的数据结构的统称。堆通常是一个可以被看做一棵树的数组对象。堆总是满...

  • PHP的SPL标准库

    SPL标准PHP类库。是php内置的一些拓展类和拓展接口,其内容包含数据结构、迭代器、接口、异常、SPL函数,文件...

  • PHP中的一些标准库

    很多PHPer都不知道PHP有着自己的一些标准库,官网已经列出了SPL的PHP标准库 标准库中主要的一些数据结构 ...

  • php SPL(PHP标准库讲解)

    PHP SPL标准库 官方解释: SPL 提供了一套标准的数据结构。它们按底层实现进行分组, 通常定义了它们的一般...

  • PHP标准库 (SPL)实现常用数据结构

    php标准库(spl) 栈:先进后出,后进先出 $q = new SplStack();$q[] = 1;$q[]...

  • PHP SPL标准库之双向链表

    简介 SPL是用于解决典型问题(standard problems)的一组接口与类的集合,包括数据结构、迭代器、接...

  • 章节八:基本数据结构二

    SPL(Standard PHP Library,PHP标准库)中并无树和图数据结构的实现,考虑到实用性,同时呼应...

  • PHP基础 -- 类自动载入

    使用PHP标准库SPL,中的自动载入功能,自动require类文件 创建4个文件 index.php主入口文件 C...

网友评论

    本文标题:PHP SPL标准库之固定大小数组

    本文链接:https://www.haomeiwen.com/subject/utlekxtx.html