Search Unity

Intensive Math Library

Discussion in 'Editor & General Support' started by KTC, Oct 22, 2014.

  1. KTC

    KTC

    Joined:
    Jan 7, 2013
    Posts:
    7
    I have a simple unity app. This app has a single scene a few simple ui elements and an intensive math library. The math library Needs to work for a few seconds and than based on the output I use unity to show a 3d graphs.

    l need to optimize the calculation time under unity for great user experience. If I use Native ios app, the library takes 3secs. If I use the same lib with unity it takes 7 secs.

    Is there a way I can prioritize my library during the calculation time? I am ok to make ui freeze, show no new frames etc. Basically I like to stop unity and give all the available CPU to my library during the calculation time.
     
  2. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    The only thing you can do is freeze the Physics step with Time.timeScale = 0. This won't stop rendering though, just physics update. If you don't have any physics going on at the time, it's probably not going to help.

    If it's taking 7 seconds with nothing else loaded in Unity at that point, probably the only thing you're going to be able to do is to optimize your calculation.

    1) Use multi-threading. You can't call any Unity functions in other threads, but you can use them for your own calculations.
    2) Write a native plugin to do the calculations (Unity Pro officially required, but there are ways around this). C++ runs about 2x the speed of C# in Unity currently.
     
  3. KTC

    KTC

    Joined:
    Jan 7, 2013
    Posts:
    7
    Dear Guvaman,

    Thanks for the answer. My lib is already written in c++ and it is a native plugin.

    Would unityPause() help?

    Another option I am considering is to reduce frame rate to 1 during the calculations.
     
  4. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,625
    Okay,
    Well, I haven't done iOS development so I won't be able to help further, but the description of UnityPause does sound like what you need.

     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,675
    Hi,

    if you call your math library from Unity main thread, you are effectively freezing Unity - it won't do anything while your code is executing, as it thinks it's still executing your scripts.
     
  6. KTC

    KTC

    Joined:
    Jan 7, 2013
    Posts:
    7
    Unity Pause does work well. Calling from main thread is not useful since the library is multithreaded and it returns immediately. So it won't be blocking Unity.