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 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
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 (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)
}
Use Splfixedarray:
$arr = new Splfixedarray (4);
$arr [0] = ' php ';
$arr [1] = 1;
$arr [3] = ' python ';
Traversal, $arr [2] is null
foreach ($arr as $v) {
Echo $v. Php_eol;
}
Get array length
echo $arr->getsize (); 4
Increase the length of an array
$arr->setsize (5);
$arr [4] = ' New one ';
Catch exception
try{
echo $arr [10];
catch (RuntimeException $e) {
echo $e->getmessage ();
}
performance testing of Splfixedarray and array
The PHP documentation specifically describes:
The Splfixedarray class provides the main functionalities of the array. The main differences between a splfixedarray and a normal PHP array is this the splfixedarray is of fixed length and allow s only integers within the range as indexes. The advantage is that it allows a faster array implementation.
So when dealing with large, numerically indexed arrays, you should use Splfixedarray instead of the normal array.
Here is the test code:
<?php
function Pr () {
$params = Func_get_args ();
$env = Php_sapi_name ();
if ("cli" = $env) {
foreach ($params as $key => $value) {
Echo $value;
}
} else {
foreach ($params as $key => $value) {
echo "<pre>";
Print_r ($value);
echo "</pre>";
}
}
//To print out the output
<?php
Require DirName (__file__). " /function.php ";
for ($size =1000; $size <=50000000; $size *=2) {
PR (php_eol. "Testing Size: $size". PHP_EOL);
for ($s = Microtime (true), $container = Array (), $i = 0; $i < $size; $i + +) {
$container [$i] = NULL;
}
PR ("Array ():". (Microtime (true)-$s). PHP_EOL);
for ($s = Microtime (true), $container = new Splfixedarray ($size), $i = 0; $i < $size; $i + +) {
$container [$i] = NULL;
}
PR ("Splarray ():". (Microtime (true)-$s). PHP_EOL);
}
But on my test machine, this piece of code has a very strange result:
What is the reason for this: Is it because the virtual machine memory is too small to cause the final splfixedarray to be inserted with a continuous GC to cause the time to be too long?