Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dpoly · Apr 27, 2017 at 02:57 AM · cpu usagec# dll

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikael-H · Apr 27, 2017 at 06:00 AM 0
Share

By outside unity do you use mono or visual studio?

avatar image dpoly Mikael-H · Apr 27, 2017 at 08:42 AM 0
Share

Visual Studio 2016 Pro. Performance analysis tools have helped a lot to get this far.

avatar image Kossuranta · Apr 27, 2017 at 06:46 AM 0
Share

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)

avatar image dpoly Kossuranta · Apr 27, 2017 at 08:44 AM 0
Share

Yes, but there is virtually no frame overhead. The game runs at 65+ fps until the AI kicks in.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dpoly · Apr 27, 2017 at 08:50 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Score extraction from mysql database in c# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges