PHP SPL Standard library Splfixedarray use instance _php instance

Source: Internet
Author: User
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) {  
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.