Example of using SplFixedArray in the php spl standard library

Source: Internet
Author: User
Tags spl

Example of using SplFixedArray in the php spl standard library

This article mainly introduces the use of SplFixedArray in the php spl standard library. SplFixedArray is mainly used to process array-related functions. It is fixed-length and faster than normal Array Processing, for more information, see

SplFixedArray is mainly used to process array-related functions. Unlike Common php arrays, SplFixedArray is an array with fixed length and key names, the advantage is faster processing than normal arrays.

Check my local Benchmark test:

?

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 result is 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

In general, SplFixedArray is 20% ~ Faster than php array ~ 30%, so if you are dealing with a large number of fixed-length arrays, it is strongly recommended to use.

The summary of the SplFixedArray class is 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)

}

Use SplFixedArray:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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;

}

 

// Obtain the array Length

Echo $ arr-> getSize (); // 4

 

// Increase the array Length

$ Arr-> setSize (5 );

$ Arr [4] = 'new one ';

 

// Capture exceptions

Try {

Echo $ arr [10];

} Catch (RuntimeException $ e ){

Echo $ e-> getMessage ();

}

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.