Splfixedarray is mainly dealing with array-related main functions, unlike normal PHP array, it is fixed-length, and the number is the key name of the array, the advantage is more than normal array processing faster.
Check out my benchmark test for this machine:
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;
}
The results are as follows:
Testing size:10000
Array (): 0.004000186920166
Splarray (): 0.0019998550415039 testing
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
Normally splfixedarray is faster than PHP array 20%~30%, so if you are dealing with a large number of fixed-length arrays, it is strongly recommended.
The Splfixedarray class summary is as follows:
Splfixedarray implements iterator , arrayaccess , countable {
* * method
/public __construct ([int $ Size = 0]) public
int count (void) public
mixed current (void) public
static Splfixedarray FromArray (a Rray $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 of Fsetset (int $index, mixed $newval) public
void Offsetunset (int $index) public
Void Rewind (void)
P ublic int setSize (int $size) public
array ToArray (void) public-
bool Valid (void) public
void __wake Up (void)
}
Use Splfixedarray:
$arr = new Splfixedarray (4);
$arr [0] = ' php ';
$arr [1] = 1;
$arr [3] = ' python ';
Traversal, $arr [2] is a null
foreach ($arr as $v) {
echo $v. Php_eol;
}
Gets the array length
echo $arr->getsize ();//4
//increase array length
$arr->setsize (5);
$arr [4] = ' new one ';
Catch exception
try{
echo $arr [ten];
} catch (RuntimeException $e) {
echo $e->getmessage ();
}