views:

184

answers:

2

i need a structure like this

    array(){
    [0] => array(){
           [0] => array(){ 
                         // this array will have 'n' values(n is large, like 2000)
                         }
           [1] => array(){ 
                         // this array will have 'n' values(n is large, like 2000)
                         }
                   }
    .
    .
    .
[n] => ............
}

n arrays will each have a 2 element array, where each element has an array of n values.

I used $list[$m][0][$n] and $list[$m][1][$n] inside 2 for loops where $m,$n vary from 0...2000

this crosses the allowed memory size.. i can change the size in php.ini, but i want to optimize my memory usage and not change the limit.

will using objects help ?

Please provide some sample code to understand. Thank you.

+3  A: 

Assuming that the net weight of your data crosses the memory limit, I can't see how objects could be of help. For the purposes of data storage, they're just a different form of notation, really. Maybe one method will save a byte per piece over the other - I don't know, but if there's a difference, my bet is objects are more expensive.

But I think the general question is what you are trying to do with that lot of data? Could it be feasible to store parts of it to disk or the database, and have only a part of it in memory?

Pekka
i am trying to implement the stable marriage algorithm in php where i have all the men and women in a single list ..
srk
I agree... if its coming from a db then you should be paging with limit and offset - if its coming from file then some sort of line/block parsing would be called for.
prodigitalson
@srk I don't know that algorithm, maybe you want to elaborate in your question?
Pekka
Ok.. let me explain you what we need to do... we have men and women.. both have their preferences towards each other.. the array $list[man_id] will need to have the woman_id and his preference score for her for each woman. so do you understand the need for the above array.. Can you suggest a better structure.. both the id's and the preference score are integers..and similarly the $list[woman_id] will have men_id's and scores
srk
+5  A: 

Using objects will most likely not help (it might even be worse).

What you need to do, in a case such as this one, is re-think :

  • either your design : there must be another way to achieve what you want
    • possibly, using another algorithm ?
    • or storing some "temporary data" elsewhere than in memory ? In an SQLite database, for instance ?
  • or the language you'll use for your script
    • PHP is not always the best tool for the job.
Pascal MARTIN