What are the advantages of using Splfixedarray instead of an array?

Asked

Viewed 80 times

4

What are the advantages of using Splfixedarray instead of an array?

What are the advantages of fixing a value to an "array" (in PHP) ?

Example with array:

$arr = array();

$arr[0] = 'Stack';

$arr[1] = 'Overflow';

$arr[2] = 'blablabla';

Example withSplFixedArray

$arr = new SplFixedArray(2);

$arr[0] = 'Stack';

$arr[1] = 'Overflow';

$arr[2] = 'blablabla'; // RunTimeException: Index invalid or out of range 
  • 1

    "a value for an array" do you mean a size? If that’s the advantage it takes up exactly the memory space you want.

  • 1

    Splfixedarray is faster than matrices, and has a lower memory consumption.

  • Why?????????

  • Because tests have already been done, and more tests, from one look in this article, will know why!!!

1 answer

2


The advantage is that it allows a rapid implementation of Array. As illustrated by the benchmarks performed by the author of this article: inserir a descrição da imagem aqui

In addition, Splfixedarray’s memory consumption is actually lower, but only noticeable for a large amount of array elements. Because Splfixedarray is technically an instance of a class, unlike traditional arrays.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.