Performance comparison between a regular PHP array and SplFixedArray

one of the key questions we encountered in the remaining segment turned into, why ought to we use SplFixedArray in preference to personal home page arrays? we’re now prepared to explore the solution. We got here across the concept that Hypertext Preprocessor arrays are truly now not arrays rather than hash maps. allow us to run a small example code in Hypertext Preprocessor five.x model to peer the reminiscence utilization of a Hypertext Preprocessor array.
allow us to create an array with a hundred,000 particular php integers. As i am running a sixty four bit gadget, I count on every integer to take eight bytes every. So we can have around 800,000 bytes of memory fed on for the array. right here is the code:

$startMemory = memory_get_usage();
$array = range(1,100000);
$endMemory = memory_get_usage();
echo ($endMemory - $startMemory)." bytes";

If we run this code in our command spark off, we can see an output of 14,649,040 bytes. sure, it’s far accurate. The reminiscence…

More examples using SplFixedArray

Introduction ¶

The SplFixedArray class provides the main functionalities of array. The main difference between a SplFixedArray and a normal PHP array is that the SplFixedArray must be resized manually and allows only integers within the range as indexes. The advantage is that it uses less memory than a standard array.

class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable {
/* Methods */
public __construct(int $size = 0)
public count(): int
public current(): mixed
public static fromArray(array $array, bool $preserveKeys = true): SplFixedArray
public getSize(): int
public key(): int
public next(): void
public offsetExists(int $index): bool
public offsetGet(int $index): mixed
public offsetSet(int $index, mixed $value): void
public offsetUnset(int $index): void
public rewind(): void
public setSize(int $size): bool
public toArray(): array
public valid(): bool
public __wakeup(): void

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x