PHP SPL Standard library Splfixedarray usage Example _php Tutorial

Source: Internet
Author: User
Tags spl

PHP SPL Standard library Splfixedarray usage examples


This article mainly introduces the PHP SPL standard library Splfixedarray Use instance, Splfixedarray mainly deal with the main function of the array related, it is fixed-length, faster than ordinary array processing, the need for friends can refer to the next

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:

?

1

2

3

4

5

6

7

8

9

10

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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 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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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)

}

Using Splfixedarray:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

$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 array length

$arr->setsize (5);

$arr [4] = ' New one ';

Catching exceptions

try{

echo $arr [10];

} catch (RuntimeException $e) {

echo $e->getmessage ();

}

http://www.bkjia.com/PHPjc/1000112.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000112.html techarticle PHP SPL Standard library Splfixedarray Use example this article mainly introduced the PHP SPL standard library Splfixedarray Use instance, Splfixedarray mainly handles the array related main function, it is the solid ...

  • 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.