Is there a C ++ equivalent of std :: vector in PHP?

Is there a lite replacement for PHP array to use when I don't need any of the related array functions? From what I know, array is a hash map inside, which is excessive and inefficient for storing a simple array of elements. If PHP had a class or programming construct similar to C ++ std::vector , that would be just great.

Link: http://www.cplusplus.com/reference/vector/vector/

+7
performance arrays php
source share
2 answers

Look at the SPL databases. An example is http://www.php.net/manual/en/class.splfixedarray.php , which is faster than a regular array.

http://www.php.net/manual/en/spl.datastructures.php

+9
source share

No, PHP does not have the strict equivalent of std::vector .

We just use multipurpose arrays or one of the additional datastructures in SPL , namely SplFixedArray , ArrayObject and some Heaps and Stacks , but none of them are actually equivalent.

The closest I can think of to save memory is the PECL extension for Judy arrays :

PHP Judy implements sparse dynamic arrays (also called Judy arrays). This extension is based on the Judy C library. The Judy array only consumes memory when it is populated, but can grow to use all available memory if desired. Judy's main advantages are scalability, high performance, and memory efficiency.

It supports the following modes:

  • BITSET - Define Judy array as bitset with keys as Integer and Values ​​as logical
  • INT_TO_INT - Define a Judy array with key / values ​​as Integer and Integer.
  • INT_TO_MIXED - Define a Judy array with keys as Integer and values ​​of any type.
  • STRING_TO_INT Define a Judy array with keys as String and values ​​as Integer and Integer.
  • STRING_TO_MIXED - Define a Judy array using the keys as a string and values ​​of any type.

You probably want INT_TO_MIXED . As I said, this is the closest thing I can think of. This is not the same. I have never used it before, so I can’t say if it meets your requirements in terms of efficiency.

You can view the source code http://lxr.php.net/xref/PECL/Judy/

+3
source share

All Articles