• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by Ludeme Games · Oct 01, 2012 at 07:39 AM · c#performanceoptimizationdllmono

Dll call 10x slower in Unity

I've written a Monte-Carlo based AI for an abstract strategy game (Eg: a game like Chess) and compiled it into a dll. When I run this code from a console application it runs fine, but from Unity it takes 10x longer to get the same result.

There is not a lot of interop between Unity and the dll. Here is the basic architecture...

On start, Unity creates a thread for the game to run in. When the game is instantiated 'controllers' can be attached to the players to feed the game input. Likewise 'viewers' can be attached that notify of a new game state (to display the UI, for instance) This is how communication with Unity is done - only when the game asks for a move or notifies that a move has been made.

The MonteCarlo part is a 'controller'. It fires up 8 threads and churns on a bunch of numbers for a while without leaving the dll. That's about it... pretty simple.

How could it be that it is running so much slower in Unity?

Comment
gregzo
vexe
tomix1023

People who like this

3 Show 8
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 mattbenic · Oct 02, 2012 at 08:56 AM 1
Share

How are you interacting with the dll? Is it directly in your project and functions being called directly from C# code, or are you using UnitySendMessage/DllImport? This shouldn't matter too much if you're only doing it occasionally (which it sounds like might be the case in your project) but if it's happening regularly it would seriously slow down your app.

Also, how are you doing your multithreading from Unity? Could you post some code?

avatar image Ludeme Games · Oct 02, 2012 at 10:51 AM 0
Share

I am importing the dll into the assets folder and calling it from C#. Also, I am only starting a thread in Unity which calls into the dll once. I (essentially) pass the call an Action to perform when the number crunching is over. All of the timing is done within the thread - before the Action is called.

It's hard to post code right now without including a few hundred source files... but if this issue persists I'll spend a while to make a stripped down version.

avatar image whydoidoit · Oct 02, 2012 at 10:54 AM 1
Share

How many processors do you have?

avatar image mattbenic · Oct 02, 2012 at 11:13 AM 1
Share

Yeah then it really does make no sense, since there's only one point of interaction between Unity and the dll. I assume this is on an Editor or PC build right? Not mobile platforms?

[Edit] NM, saw your comment about processors.

avatar image whydoidoit · Oct 02, 2012 at 11:20 AM 1
Share

Can you see how busy it thinks the cores/threads are?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by whydoidoit · Oct 03, 2012 at 05:52 AM

Could it be memory allocations? Garbage Colection is different under Unity.

Comment
vexe

People who like this

1 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 Ludeme Games · Oct 05, 2012 at 03:08 AM 1
Share

Very well could be. It looks like this can be profiled: http://msdn.microsoft.com/en-us/library/cc713687(v=vs.90).aspx I'll update soon.

avatar image

Answer by tomix1023 · Oct 19, 2018 at 07:55 AM

I am experiencing the very same 10x slowdown in calls to my native code in Unity as well (Even on pure computational calls, where nothing is allocated or something like that). This happens both on Windows and Linux with Unity 2017.2 - 2018.2.

To work around this 10x slowdown, I have to reimport the *.cs Files in Unity after every launch. This brings the performance back to where it should be in the first place.

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

DLL compilation 1 Answer

how to connect to mssql in unity for hololesn (Build Setting Error) 0 Answers

Colliders vs Callbacks: Which is better for performance in my case? 0 Answers

The requested feature is not implemented 0 Answers

Multiple Cars not working 1 Answer


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