btherl Posted September 5, 2006 Share Posted September 5, 2006 PHP's arrays have high overhead, particularly for complex structures such as[code]$a[0] = array( 'host' => 'www.phpfreaks.com', 'path' => 'forums', 'file' => 'index.php', 'args' => 'action=post', 'count' => 5, 'potatoes' = true, ... etc etc);[/code]Are there any extensions for php which allow for more efficient data storage, at the expense of reduced flexibility? I am looking for something similar to a C structure, where data is tightly packed, and names for the elements do not need to be stored.The structure needs to support:[list][*]Inserting at the end of the array[*]Fetching data from any location in the array (indexed by integers, like a C array)[*]Sorting (this could be complex)[/list]My main concern is that the labels for each data item (such as 'host', 'path') are repeates for EVERY element in my array $a. This is a painful waste of space. My secondary concern is that I do not want to use an entire zval to store a simple boolean, or a simple integer. I would like to pack these values more tightly, even if accessing them becomes more costly.Thanks for any advice :) Quote Link to comment https://forums.phpfreaks.com/topic/19744-efficient-array-storage-for-php/ Share on other sites More sharing options...
Jenk Posted September 5, 2006 Share Posted September 5, 2006 in a word.. No.Besides, PHP uses C directly for all primitives, only exception to the rule is string key is allowed. (Uses a secondary array for the keys)Why is it such a concern? What exactly are you doing with arrays to make them so huge? Quote Link to comment https://forums.phpfreaks.com/topic/19744-efficient-array-storage-for-php/#findComment-86217 Share on other sites More sharing options...
Zane Posted September 5, 2006 Share Posted September 5, 2006 [quote]I am looking for something similar to a C structure, where data is tightly packed, and names for the elements do not need to be stored.[/quote]Well, you don't HAVE to store the names of the elementsI think the default structure of an array is sorted by indexunless one happens to have a stringed key.I don't know if I'm on the right track exactly to what you're asking but....if you don't want an associative array, one with string indecesand want to avoid the 'repeats'maybe have a shema element at the beginning of the array to aesthetically please you're coding....or whateverbut overall...you don't need the keysand as for your question about adding to the end of the arrayit should still work Quote Link to comment https://forums.phpfreaks.com/topic/19744-efficient-array-storage-for-php/#findComment-86219 Share on other sites More sharing options...
btherl Posted September 5, 2006 Author Share Posted September 5, 2006 Thanks for the comments!The reason it's a concern is that I want to process large amounts of data, and I want to sort the entire data set by various keys. The larger data sets are 250k rows, and possibly larger. The data would fit in memory, but the overhead of storing the data is currently much larger than the data itself.Jenk, I'm not sure what you mean by "PHP uses C directly for primitives". As I understand, php uses hash tables for arrays.Zanus, there looks to be a small memory benefit to using integers instead of strings, based on small tests. I will try it and see if it measurably improves memory usage, thanks :)I don't think the underlying structure is different for numerically indexed arrays however. It would be nice if it was :) But memory usage is the same for numerically indexed arrays and arrays indexed by short strings. I would expect lower memory usage if a simple array was being used. Quote Link to comment https://forums.phpfreaks.com/topic/19744-efficient-array-storage-for-php/#findComment-86231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.