Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any downside to creating lots of class instances?

I have lots of variables to keep track of in my game, and I'm wondering if there's any downside to creating lots of class instances:

event_1 = event(name, chance, [people])
event_2 = event(name, chance, [people])
...
events = [event_1, event_2]

instead of just creating a nested list?

events = [
    [name, chance, [people]],
    [name, chance, [people]],
    ...
    ]

other than the trading some instance names for ease of use in the case of classes, should I be worried about something like performance penalties or anything like that?

Edit: I mentioned the ease of use, what I'm wondering is: do classes in a list use more resources than a nested list or it's the other way around?

like image 376
Kia Azad Avatar asked Nov 19 '25 14:11

Kia Azad


1 Answers

My advice would be to first optimise for readability and maintainability of your code. Focus on performance only when it demonstrably becomes an issue. Once you know it's an issue, you'll be able to measure the system in its entirety, identify bottlenecks and fix them.

You may find that:

  • performance isn't an issue at all; or
  • it is an issue, but the bottleneck isn't at all where you thought it would be (from experience, this happens all the time).

With this in mind, I would favour the first approach over the second.

like image 77
NPE Avatar answered Nov 21 '25 04:11

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!