- Home /
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?
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 UnitySend$$anonymous$$essage/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?
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 ti$$anonymous$$g 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.
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] N$$anonymous$$, saw your comment about processors.
Answer by whydoidoit · Oct 03, 2012 at 05:52 AM
Could it be memory allocations? Garbage Colection is different under Unity.
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.
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.
Your answer
Follow this Question
Related Questions
Colliders vs Callbacks: Which is better for performance in my case? 0 Answers
DLL compilation 1 Answer
Use Mono in Unity 1 Answer
Distribute terrain in zones 3 Answers
how to connect to mssql in unity for hololesn (Build Setting Error) 0 Answers