簡介
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下還是挺有用,能夠提升性能!