- Home /
Why does my CPU-heavy A/I run so slow in Unity?
Polygame is a player for abstract games and puzzles. To play well the A/I does a tree search on available moves to find the best. For a sample game it needs to consider about 100,000 positions and that takes about 5.5 seconds outside Unity. In Unity that identical code takes 19.0 seconds to run. Why?
The code is identical: Polygame is built as a DLL library and imported into Unity. I'm testing the same binary.
The code is written to do incremental update using a Coroutine, and I can vary the parameters. It makes almost no difference whether the frame count is 1, 2, 5 or 20, the total elapsed time to do the tree search is the same. It isn't frame overhead causing the problem.
So what is it? And more important, is there anything I can do to fix it? A slow-down of nearly x4 is pretty bad.
Visual Studio 2016 Pro. Performance analysis tools have helped a lot to get this far.
Have you tried running it without coroutine? I know that it will make your game freeze, but I'm just interested if that makes any difference and if it does how much. Possible that your main game loop is taking too much resources and there is no CPU time left? (CPU heavy game or running at ridiculous frame rates)
Yes, but there is virtually no frame overhead. The game runs at 65+ fps until the AI kicks in.
Answer by Mikael-H · Apr 27, 2017 at 05:59 AM
Try running the unity profiler to see where in the code the time is used up and if there is any garbage being generated. The fact that it is slower in unity might indicate that it has to do with the mono runtime (I assume that the 5.5 second test is done with standard .Net runtime?).
The mono runtime is slower in some cases, especially where there is garbage generated, but there may be other cases as well were it might be slower so running the profiler just might give you the hint you need. Maybe you wrote some code that the standard .Net runtime handles just fine but the mono runtime does slow. In that case it might be possible to fix. Or if you are unlucky the mono runtime is just slower or the overhead of the Unity engine is killing your performance.
I know where the CPU goes inside my code (VS performance analyser). What I don't know is what Unity is doing, or $$anonymous$$ono, under the covers. Unity profiler: good idea, I haven't tried that. GC load: should be quite low. The tree search builds lots of objects but doesn't free them, much. I can check that out in VS. BTW the figures I quoted are for the editor. A desktop build runs about 10s (ins$$anonymous$$d of nearly 20s), so the overhead is only really about 2x.
Your answer
![](https://koobas.hobune.stream/wayback/20220612120736im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to access functions and variables in from one script in another. 1 Answer
No me muestra el cuello de botella en Unity Profiler 1 Answer
Is it logic that Unity 5 when adding terrian the cpu is getting to 100% ? 0 Answers
Why my project takes me so much CPU capacity to run when playing? 1 Answer