8

I need a simple array that uses minimum memory in PHP. I want the exact C++ equivalent of an allocated block of memory where you can iterate using indices only. I found out that arrays in PHP use more memory than let's say : size*type_size (I guess for storing key values, etc). Is there anything more unsophisticated and simple ?

EDIT:

Thank you all.

Yes, I thought the idea of the string immediately after posting the question. I needed a boolean array, so that seemed to work. It's just a little slower to get/set it's characters.

Judy arrays also seem interesting but I haven't tried it yet.

I've tried SplFixedArray but it seemed that it used the same amount of memory to normal arrays (except if I've missed sth on the way).

2
  • 2
    How can you possibly need more speed than whats given from array()? If you need more speed from php then what array() can give you, you're probaly using the wrong language. Commented Nov 26, 2011 at 11:25
  • 1
    This isn't even possible unless you restrict the items to a particular type (and here, "type" means "C type" - fixed sizeof). And then it's still a mess. And depending on the type of items and the PHP implementation, it may lead to boxing/unboxing on all accesses, voiding much of the potential speed benefit. Commented Nov 26, 2011 at 11:26

5 Answers 5

10

You could use the SplFixedArray which seems to meet your demands.

Sign up to request clarification or add additional context in comments.

1 Comment

The SplFixedArray has horrible performance due to it being a PHP object and having major creation overhead. That being said, once it's created it has better memory and speed.
9

I need a simple array that uses minimum memory in PHP.

The speediest array type in PHP is actually the string. It also is the least memory-hungry; the closest you get to a C equivalent data structure.

 $byte = ord($string[123]);

It's indexed. But the limitation is of course that it only works on byte values. (You didn't exactly elaborate on your needs. So here is your generic answer.)

As alternative to the fixed length SplFixedArray PHP also has an extension for Judy arrays. They are associative, but conserve memory in comparison to PHPs; and are supposedly a bit speedier. (Doesn't make much sense in a scripting language to care about that, but hey.)

8 Comments

Thanks. Yes, I thought the idea of the string immediately after posting the question. I needed a boolean array, so that seemed to work. It's just a little slower to get/set it's characters. Judy arrays also seem interesting. I've tried SplFixedArray but it seemed that it used the same amount of memory to normal arrays (except if I've missed sth on the way).
@NoOne: If you are dealing with booleans, then applying "1" and "0" should work just as well as "\0" or "\1". Thus you trade readability for speed. The SplFixedArray implementation requires one zval less per entry (normally one for the key and one for the value), but if you just store booleans it won't save you much.
@mario. so how do we go about creating a string of a specified length efficiently? For example, a string of length 40k?
@Pacerier Most commonly str_pad is used for that. For example str_pad("", 40960, "\0") for 40K of NUL bytes.
@mario, would str_repeat perform better?
|
2

http://php.net/manual/en/class.splfixedarray.php

The SplFixedArray class provides the main functionalities of array. The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it allows a faster array implementation.

Closest you can get.

2 Comments

How much faster? A speedup of 1% doesn't matter. A speedup of 25% can make or break a program or website.
You are answering a decade old question , the memory and CPU power landscape have changed immensely. Based on this answer today a $35 computer provides magnitudes-comparable performance to ordinary computers of the era of the question. SO's policy is to keep these questions and answers as-is. meta.stackoverflow.com/q/411296/308851
1

You have no choice.
All the arrays in PHP are the same ordred maps anyway.

If you experience any memory issues with the arrays, I'd sugget to use some more relevant technology.
PHP is designed to process relatively small amouints of data per user request to web-server.

Comments

0

The Judy library and its PHP extension is a very good way to use array in PHP.

2 Comments

This answer needs to be revised or deleted, since the link is dead. PHP Judy is a PECL extension for the Judy C library implementing dynamic sparse arrays.
True. The correct link should be pecl.php.net/package/judy (This package is not maintained)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.