dj-kenpo Posted July 23, 2007 Share Posted July 23, 2007 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 https://forums.phpfreaks.com/topic/61311-how-much-~memory-does-a-simple-array-eat-anyone-know/ Share on other sites More sharing options...
btherl Posted July 23, 2007 Share Posted July 23, 2007 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 https://forums.phpfreaks.com/topic/61311-how-much-~memory-does-a-simple-array-eat-anyone-know/#findComment-305087 Share on other sites More sharing options...
cooldude832 Posted July 23, 2007 Share Posted July 23, 2007 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 https://forums.phpfreaks.com/topic/61311-how-much-~memory-does-a-simple-array-eat-anyone-know/#findComment-305093 Share on other sites More sharing options...
dj-kenpo Posted July 23, 2007 Author Share Posted July 23, 2007 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 https://forums.phpfreaks.com/topic/61311-how-much-~memory-does-a-simple-array-eat-anyone-know/#findComment-305096 Share on other sites More sharing options...
cooldude832 Posted July 23, 2007 Share Posted July 23, 2007 That is about the only disadvantage of PHP is that it is way over engineered because its so function orientated that it becomes a resource hog when you try and run large amount of code. Like if you had that array in C/C++ for say it would be easier on it than in php. Link to comment https://forums.phpfreaks.com/topic/61311-how-much-~memory-does-a-simple-array-eat-anyone-know/#findComment-305100 Share on other sites More sharing options...
btherl Posted July 23, 2007 Share Posted July 23, 2007 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 https://forums.phpfreaks.com/topic/61311-how-much-~memory-does-a-simple-array-eat-anyone-know/#findComment-305119 Share on other sites More sharing options...
cooldude832 Posted July 23, 2007 Share Posted July 23, 2007 I've always dreamed of interacted C and Php in such a way, but never found that use for it so thus i never built it Link to comment https://forums.phpfreaks.com/topic/61311-how-much-~memory-does-a-simple-array-eat-anyone-know/#findComment-305121 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.