Background: I have a large 2D array of integers that I need to load into memory in PHP for each Apache request. I want it to take up less memory.
PHP stores ints in PHP_INT_SIZE
bytes, which is 32 bits on most systems. All the integers are less than 2^16, which means they could be a short int (eg in C). Am I right in thinking that storing ints as short would take up half the RAM?
Ideally I'd like to be able to do:
$s = (short) 1234; // takes up 2 bytes instead of 4
More info:
Some crazy ideas:
The size of a signed int or unsigned int item is the standard size of an integer on a particular machine. For example, in 16-bit operating systems, the int type is usually 16 bits, or 2 bytes. In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes.
A 16-bit integer can store 216 (or 65,536) distinct values. In an unsigned representation, these values are the integers between 0 and 65,535; using two's complement, possible values range from −32,768 to 32,767.
An integer must NOT have a decimal point. An integer can be either positive or negative.
The size of an int is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned ints.
I would not recommend last approach. :-)
For the quick solution, I would pack 2 your integers in 1 PHP integer using this:
$big = $int1 + ($int2<<16);
And uppack as:
$int1 = $big & 65535;
$int2 = ($big>>16) & 65535;
Also, BIG thumbs up for using shared memory. This will make your APP way faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With