Performance of instantiate and destroy for bullets
Hi all, I have read many threads here about pooling vs instantiate & destroy , but there doesn't seem to be a definitive answer.
I am working on a new game and before I get to the shooting system, I want to make sure my approach is good. I know that for mobile games, pooling objects is better for performance, but I am only developing for PC, Linux and Mac.
So, if you have machine guns firing bullets all over the place, should I simply create prefabs for the bullets and instaniate at the gun position and add force, using destroy when they collide with something?
I have tried this and it seems to work flawlessly, but then I do have a monster rig for my development machine.
What is the general consensus here? with todays modern PC's are you really going to see a hit in performance here? What if I call a GC regularly?
Or should I just stick to pooling objects instead (I can, but its a lot of extra work that I am not sure if its necessary).
Thanks for any advice
Pooling is definitely better for performance. Don't take my advice though, personally I'd rather hear what someone from Infinite ward or Bungie have to say about that.
If it helps, I've resorted to using particles for my bullets, and I'm able to have 10s of thousands on my screen at once without any major performance concerns.
Answer by Suddoha · Sep 14, 2015 at 02:53 PM
Stick to object pooling.
In general, it's good to save performance wherever you can if it's not an overkill and too much effort for your project. Even though you may not recognize a drop in performance on your system, users may recognize it and as soon as your projects starts growing, there will be other things that may profit from the time you've saved there.
Basides that, object pooling allows to re-use the objects in an efficient way as you may know, you can control the amount of these objects (you can even use this in some ways to allow further tweaking on the user side via settings) and thus keep your scenes clean.
On top of that, it's a bit more professional and you can generalize the logic so that you don't even need to re-write everything.
Calling the GC manually is not a good idea at all as far as i can recall and may even cause noticeable "hiccups".
Thanks for the input, its gives me some food for thought.
Answer by Mr_Edward · Sep 14, 2015 at 03:23 PM
The performance hit isn't that bad, and it'll run fine on older machines. Especially when your CPU isn't overloaded with other calculations from the game. If you know that your game is going to be CPU intensive, implementing object pooling might still be a good idea though.