Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

TIming how long a function takes to do its thing

Discussion in 'Scripting' started by Laurie-Athey349, Apr 28, 2013.

  1. Laurie-Athey349

    Laurie-Athey349

    Joined:
    Jan 28, 2013
    Posts:
    14
    Hi all.
    So I am doing some work with dice stats and probability generation and I want to be able to asses how long its taking my recursive function to do its thing.

    At first I went to delta time, stored its value before and after the call then worked out the difference. But I ALWAYS got 0.

    I figured that was because it was the time it took for the PREVIOUS frame and not time since the current frame began.
    So I looked up Time:
    http://docs.unity3d.com/Documentation/ScriptReference/Time.html

    Now Time.time seems like what I want. So I try the above method but replace Time.deltaTime with Time.time and I still get 0 even when I know for a fact that its taking around 7-8 seconds.

    I have re-read the above page several times and c no way of working this out. Any ideas?
     
    blox5000 likes this.
  2. damiantequila

    damiantequila

    Joined:
    Mar 8, 2013
    Posts:
    19
    Use DateTime class. This should looks like this:
    Code (csharp):
    1.  
    2. DateTime before = DateTime.Now;
    3. SomeFunction();
    4. DateTime after = DateTime.Now; 
    5. TimeSpan duration = after.Subtract(before);
    6. Debug.Log("Duration in milliseconds: " + duration.Milliseconds);
    7.  
    If function is small, it is good idea to run it multiple (100, 1000) times and divide result.
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Also, Time.realtimeSinceStartup may be a bit simpler route.
     
  4. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    If you are using C#, try the Stopwatch also.
     
  5. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Code (csharp):
    1.  
    2. void MyMethod(){
    3. Stopwatch st = new Stopwatch();
    4. st.Start();
    5. //Whatever needs timing here
    6. st.Stop();
    7. Debug.Log(string.Format("MyMethod took {0} ms to complete", st.ElapsedMilliseconds));
    8. }
    9.  
     
    Last edited: Apr 29, 2013
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Stopwatch is a .NET class, not related to C# specifically. All .NET functions and classes work in all languages.

    --Eric
     
  7. Laurie-Athey349

    Laurie-Athey349

    Joined:
    Jan 28, 2013
    Posts:
    14
    Wow, Answers!

    Amazing stuff peeps, i am gonna go try this out now.

    if its fast to try ill edit this post if not ill double post (apologies in advance)

    EDIT:

    Right, tried DateTime and that seems to return 756 everytime when it should be returning like 30,000.

    This is the code, tell me if there is a glaring error

    DateTime before = DateTime.Now;
    funky (0);
    DateTime after = DateTime.Now;
    TimeSpan duration = after.Subtract(before);
    Debug.Log ("Time taken in Milliseconds: "+(duration.Milliseconds));
     
    Last edited: Apr 29, 2013
  8. Deleted User

    Deleted User

    Guest

    use the stopwatch example that EliteMossy postet. you need to use/import System.Diagnostics to get it to work
     
    Glashtyn likes this.
  9. Laurie-Athey349

    Laurie-Athey349

    Joined:
    Jan 28, 2013
    Posts:
    14
    ok,
    Got stopwatch working but no luck with DateTime

    Here is what you need to get this working, there are a few things to mention also:
    1. If you use
    Code (csharp):
    1. Using System.Diagnostics;
    then every time you use Debug.Log it will not know whether you mean
    Code (csharp):
    1. System.Diagnostics.Debug.Log();
    or if you mean
    Code (csharp):
    1. UnityEngine.Debug.Log();
    , so DON'T include System.Diagnostics! whenever you wanna use stopwatch just use the following prefix:
    System.Diagnostics in front of Stopwatch.

    2. Didn't notice at first but you need to make an instance of stopwatch, and if you are using it lots of times, remember that you WILL need to RESET it.So proper use goes something like this:

    Code (csharp):
    1. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
    2. stopwatch.Start();
    3. myFunctionThatIWantToTime();
    4. stopwatch.Stop();
    5. Debug.Log ("Time taken: "+(stopwatch.Elapsed));
    6. stopwatch.Reset();
    if I had included System.Diagnostics it would have removed the two references to it there but added about 50 references to UnityEngine in the rest of my code everytime I used Debug.Log

    Ok, thats how I solved my problem, Hope it helps.

    Incidentally I will be putting the code for this god forsaken Dice app on my site eventually (shameless self advertising)

    http://laurieathey.webs.com/

    Not much there yet, I'm still in Uni but almost done (hopefully xD)

    Ty for all the help peeps,
    BB
    Doc
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You can just as easily qualify the Debug by using UnityEngine.Debug.Log(string). Depends on which one you're using more often.
     
  11. Cursedth

    Cursedth

    Joined:
    Oct 4, 2016
    Posts:
    27
    [For any still one venturing here after all these years, 2013 since last post :)]
    You can tell C# which Debug to use by adding
    Code (CSharp):
    1. Using Debug = UnityEngine.Debug;
    after all the Using directives to include namespaces eg
    Code (CSharp):
    1. Using System;
    2. Using System.Diagnostics;
    3. Using UnityEngine;
    4. Using Debug = UnityEngine.Debug;
    This will resolve ambiguity between classes
     
    scionious likes this.
  12. robbalian

    robbalian

    Joined:
    Nov 11, 2017
    Posts:
    1
    FYI if using TimeSpan, you should use duration.TotalMilliseconds for the total elapsed time.

    Code (csharp):
    1.  
    2. //example
    3.  
    4. TimeSpan duration = // some duration equal to 2.234 seconds
    5. duration.Milliseconds // returns 234
    6. duration.TotalMilliseconds // returns 2234
    7.  
     
    jeffdyerSRTMarine, arvzg and cephalo2 like this.
  13. jeffdyerSRTMarine

    jeffdyerSRTMarine

    Joined:
    Jan 23, 2019
    Posts:
    11