0

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.

2
  • What are you using the data for? Where are you getting it from, and does it need to be displayed? Commented Mar 23, 2010 at 19:16
  • getting the data from an input file.. one thing i forgot to mention is all the values are integers.. will that help ?? Commented Mar 23, 2010 at 19:22

2 Answers 2

5

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.
Sign up to request clarification or add additional context in comments.

Comments

3

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?

4 Comments

i am trying to implement the stable marriage algorithm in php where i have all the men and women in a single list ..
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.
@srk I don't know that algorithm, maybe you want to elaborate in your question?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.