Jump to content

how much ~memory does a simple array eat? anyone know?


dj-kenpo

Recommended Posts

just wondering what performance costs there are etc...

 

theoretical question..

 

say 500 items stored temporarily into an array variable with an id and title

 

$temp_array[$ID] = $title;

(title = varchar 150 character max)

 

it's for a page where it may,.. or may not be called... but when it is called, it could be called 1 or 10 times (it's a search page...) so I'm wondering if it's more sensible to load into memory temporarily for a split second rather than do up to 10 mysql queries for the title..

 

again, assume an array with 500 items~ is it no point worrying about the 4k or, what even less? maybe 1k?

 

I don't want to shoot myself in the foot as the site grows, I'm worried about speed and efficiency.

 

thanks for any thoughts/ opinions

Link to comment
Share on other sites

You might be interested in my post here:

 

http://btherl.livejournal.com/12875.html

 

It's around 56 bytes to create a new array entry.  More space will be used when the array is expanded (array size is doubled each time space runs out).  And storing long strings will take more space of course (one byte per string character).

 

So 500 array entries * 56 bytes = 28 kilobytes, plus a bit more for overhead.

 

You can always generate the array on demand when the first call is made, and cache it each time (if I understand your situation correctly)

Link to comment
Share on other sites

it sounds like you are echoing the titles out, is it possible to echo them out in a loop when needed and then they rewrite each time like

<?php
while($row = mysql_fetch_assoc){
echo $row['title'];
}
?>

and then its only storing a single row at a time, might want to make the query optimized for what oyu need

Link to comment
Share on other sites

I'm not echoing them. I'm storing them incase they need to be echoed later on, rather than doing on demand sql queries

 

@btherl: I just started reading your post, and it blows my mind how much memory php eats, but,... when you talk about using a serialized string instead, while you save memory, you cost cpu, and in the end, cpu is more costly than memory, but perhaps you talk about that too, I'm only a paragraph in

 

Link to comment
Share on other sites

CPU isn't always more costly than memory.  It depends on which you need more at the time :)

 

I started on an implementation of C struct style arrays for php (using the C interface).. the idea is that you specify the structure of your array using C data types, and then pass data in from php for efficient storage.  It was never finished, but the idea is good.  The cost is that access will be slower than accessing php's native data types, but if you have a lot of data, you may be willing to trade time for more space.

 

The data would be packed as C packs it, with no gaps except for those required by alignment restrictions.  You could compress a lot of types of data down to 10% or less of the php size by packing it like that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.