Search Unity

Get milliseconds for job to run in code without Complete()

Discussion in 'Entity Component System' started by jdtec, Jul 3, 2019.

  1. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Is there a way to time how long a job takes to run without completing it early outside of the editor and in release?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You can attach the profiler to builds.
     
  3. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Sorry I should have clarified; I'd like to get this data in the code.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    I'm not sure about your use case but i was curious and this seems to work fine

    Code (CSharp):
    1.         private Sampler sampler;
    2.  
    3.         /// <inheritdoc/>
    4.         protected override void OnCreate()
    5.         {
    6.            this.sampler = Sampler.Get("NarrowPhase:ProcessBodyPairsJob");
    7.         }
    8.  
    9.         /// <inheritdoc />
    10.         protected override void OnUpdate()
    11.         {
    12.             if (this.sampler.isValid)
    13.             {
    14.                 Debug.Log(this.sampler.GetRecorder().elapsedNanoseconds);
    15.             }
    16.         }
    Basically just pulling out the profiler data. This will only work in development builds (and the editor.)
     
    jdtec likes this.
  5. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Thanks @tertle that looks like a great start.

    I believe that Unity profiler can't connect to a release build? And you mention that the code above won't work in a release build.

    I'm used to only profiling release builds when working outside of Unity, after all you might be profiling and finding performance issues where there are none. Is there no way to achieve this with the Unity profiler (or via code instrumentation for jobs)?