I read in the “optimizing unity scripts” documentation page that using built-in arrays is always loads faster than using Arraylists.
My current game uses a lot of AI in the world, which I turn on and off as they’re visible on the screen. This was proving to be slower than I wanted it to be when I was using Arraylists (I was copying over GameObjects from AvailableList to VisibleList, then rendering the visible list), but when I’ve converted this to using built-in arrays, it actually seems to be quite a bit slower!
The original frame-rate was around 29fps, but the frame-rate after is around 24fps.
I haven’t changed any of the logic, and having done a check on the logic, it all still works as expected. The problem is that I was expecting a reasonable speed increase as opposed to a decrease?
This is all in C# - is there something I’m missing to unlock this extra speed??
You’ll probably have to post some code to get a proper answer to this one.
Perhaps the casting from Object to whatever your class is (Gameobject?) is cause the slowdown. I’ve used only used the built in arrays with value types (Floats) and found them very fast.
Can you run the code in normal (non-iPhone) Unity and look at the profiler?
There’s no casting going on. When I converted the arraylists to internal Arrays, they’re just arrays of the types in question; GameObject, float, whatever. Seems pretty odd, though!
I think it’s very unlikely that the choice of array type affects your framerate that much (although incorrectly using arrays can, you will have to post your code for us to find out).
Do you own a Unity 2.6 pro license? The profiler in there can really help pointing out what your main performance bottleneck is.
Built-in arrays are always faster than ArrayList, either by a little (GameObject[ ] is about 15% faster than ArrayList), or by a lot (int[ ] is about 500% faster than ArrayList). There’s simply less to do (no boxing/unboxing, fixed length, etc.) so it’s pretty much impossible for them to be slower, all other things being equal. The difference isn’t as much if you’re using generic Lists, though (int[ ] is only about 150% faster than List).