PHP SPL Standard library Splfixedarray use instance, Splsplfixedarray
Splfixedarray is mainly dealing with the main function of the array, unlike the normal PHP array, it is fixed-length, and the number is the key name of the array, the advantage is faster than ordinary array processing.
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:10000array (): 0.004000186920166SplArray (): 0.0019998550415039 testing Size:40000array (): 0.017001152038574SplArray (): 0.0090007781982422 testing Size:160000array (): 0.050002098083496SplArray (): 0.046003103256226 testing Size:640000array (): 0.19701099395752SplArray (): 0.16700983047485 testing Size:2560000array (): 0.75704312324524SplArray (): 0.67303895950317
Normally splfixedarray is 20%~30% than PHP array, so it is strongly recommended if you are dealing with a large number of fixed-length arrays.
The Splfixedarray class is summarized 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 off Setset (int $index, mixed $newval) public void Offsetunset (int. $index) public Void Rewind (void) PU Blic int setSize (int $size) public The array toArray (void) public bool Valid (void) public void __wakeu P (void)}
Using Splfixedarray:
$arr = new Splfixedarray (4), $arr [0] = ' php '; $arr [1] = 1; $arr [3] = ' python '; Traversal, $arr [2] for Nullforeach ($arr as $v) { echo $v. Php_eol;} Gets the array length echo $arr->getsize (); 4//Increase the array length $arr->setsize (5); $arr [4] = ' New one '; Catch exception try{ echo $arr [ten];} catch (RuntimeException $e) {
http://www.bkjia.com/PHPjc/998567.html www.bkjia.com true http://www.bkjia.com/PHPjc/998567.html techarticle PHP SPL Standard library Splfixedarray use instances, Splsplfixedarray Splfixedarray mainly deal with the main function of the array, unlike the normal PHP array, it is fixed-length, and ...