简体   繁体   中英

PHP arrays memory usage

I am trying to find a way to map big amount of strings to ints. I tried it using arrays and found a behaviour that I don't understand. When I index arrays by strings (array('someStirng' => 1)) it consumes less memory than vice versa (array(1 => 'someString')). Does it mean, that it's better to index arrays by strings and leave ints as values for big amount of string-int pairs or what's the catch? Why there is so big memory allocation difference?

function gen() {
    static $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < 8; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

Indexing by strings - returns 490 KB

$a = array();

for($x = 0;$x < 100000;$x++){
    $a[gen()]  = $x;
}


echo (memory_get_usage() / 1024) . ' KB';

Indexing by ints - returns 10790.2890625 KB (~22 times more than first case, but same ammount of information stored!)

$a = array();

for($x = 0;$x < 100000;$x++){
    $a[$x]  = gen();
}


echo (memory_get_usage() / 1024) . ' KB';

When I repeat your experiments using your code, I get 18 072 000 bytes for the integer indexes and 16 471 960 bytes for the string indexes. Not much difference, which can be attributed to different memory management for array keys and their values.

Using memory_get_peak_usage(true) instead produces very similar results. Calculating the difference in memory usage right before and right after the for loop, I get 18 087 936 bytes with integer indexes and 16 515 072 bytes with string indexes.

That's a small difference which could be explained by different internal memory management for array keys and for array values. Perhaps since array keys are limited to scalars and array values aren't, PHP can optimize somewhere.

In any case, like @ed-heal said, use the best data structure for whatever you're trying to do. The memory usage is probably not that important and if it is, PHP might not be the tool for the job.

This loop

 for ($i = 0; $i < 8; $i++) {

has 8 times to do its business

Where as this loop

for($x = 0;$x < 100000;$x++){

Does it a little more!

Now let me think where the problem is ...

EDIT

Just noticed that the sands have shifted.

Use whatever data structure is appropriate for the task in hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM