PHP SPL標(biāo)準(zhǔn)庫之固定大小數(shù)組

簡介

SplFixedArray,字面意思就是固定大小的數(shù)組,意思就是在定義的時(shí)候就要確定大小,有點(diǎn)C語言基礎(chǔ)的人估計(jì)都知道在C語言里面數(shù)組的大小是固定的,而PHP則隨意很多,代價(jià)就是性能,所以這個(gè)數(shù)組比那個(gè)性能高很多,這里有一組數(shù)據(jù):

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.實(shí)例

<?php

namespace SPL;

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

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

從使用上說,fixed 數(shù)組和普通數(shù)組大部分沒有區(qū)別,最大的問題就在于你必須先確定數(shù)組大?。∧承┣闆r下還是挺有用,能夠提升性能!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容