Or ways to get better run time than a nested loop?
How would you design this?
I hope this is the place to ask semi-vague software engineering questions.
My simple simulation is like this:
There is a population of X number of people. Every day, people go to work, which creates some quantity of something. After work, they go and buy some random thing that they want or need that someone else made.
What I have tried:
In my first prototype, the population was a list of People. Each person had methods: work() and buy() In the buy() method, the Person loops through each item on the market, breaking out of the loop as soon as it found what it needed.
The problem is that every day the market gets larger and larger, so looping through it gets longer and longer. Since the simulation is looping through every person, and then through every item, this can get very slow.
In my second prototype, I tried to use a database. Here is a vague sketch of it.
I haven't been able to write code that avoids looping, so it is still slowing down rapidly.
What approaches would yield good results?
I am not sure how to avoid the nested loop of population and market.
How would you design this so it runs smoothly even as the population and market gets larger?
What people buy is a set of random things, plus, food, clothing and housing. These don't change, so they search the market for them when they don't have them in their inventory. So I still have to search for specific things, even though they are randomly generated for each person. There are also needs that everyone has.
Things depreciate over time, so the market stabilizes eventually, but certain conditions affect this stabilization. For example, if people cannot take on debt, the market grows much faster than it would if people can borrow to buy things.
The goal of the simulation is an educational game about currency. A certain level of realism is desirable, but not the primary goal.
I appreciate the advice so far on this vague question. This will help me when I go back to the drawing board.