How can I optimize the creation of hundreds of thousands of objects that barely differ from each other?

StackOverflow https://stackoverflow.com/questions/18819491

質問

In my application, I create 153600 objects on startup, one for each point onscreen (320 * 480 = 153600). Each object is largely the same, so I've been thinking of ways to use that fact to speed up their creation. Is there a pattern or recommended way of dealing with this?

役に立ちましたか?

解決

Sounds like you might want to check out the flyweight pattern. If the separate points share a lot of data, you basically create the data they share once, and share it among the points, rather than create identical copies for each point:

http://en.wikipedia.org/wiki/Flyweight_pattern

You still have to create ~150k point objects, but you might only need to create ~1k different states that are shared among them, while the point objects themselves merely maintain a reference to shared state + only information that can't be shared.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top