TIming how long a function takes to do its thing

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:

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?

1 Like

Use DateTime class. This should looks like this:

DateTime before = DateTime.Now;
SomeFunction();
DateTime after = DateTime.Now;	
TimeSpan duration = after.Subtract(before);
Debug.Log("Duration in milliseconds: " + duration.Milliseconds);

If function is small, it is good idea to run it multiple (100, 1000) times and divide result.

4 Likes

Also, Time.realtimeSinceStartup may be a bit simpler route.

2 Likes

If you are using C#, try the Stopwatch also.

void MyMethod(){
Stopwatch st = new Stopwatch();
st.Start();
//Whatever needs timing here
st.Stop();
Debug.Log(string.Format("MyMethod took {0} ms to complete", st.ElapsedMilliseconds));
}

Stopwatch is a .NET class, not related to C# specifically. All .NET functions and classes work in all languages.

–Eric

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));

use the stopwatch example that EliteMossy postet. you need to use/import System.Diagnostics to get it to work

1 Like

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 Using System.Diagnostics; then every time you use Debug.Log it will not know whether you mean System.Diagnostics.Debug.Log(); or if you mean 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:

System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
myFunctionThatIWantToTime();
stopwatch.Stop();
Debug.Log ("Time taken: "+(stopwatch.Elapsed));
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

4 Likes

You can just as easily qualify the Debug by using UnityEngine.Debug.Log(string). Depends on which one you’re using more often.

[For any still one venturing here after all these years, 2013 since last post :)]
You can tell C# which Debug to use by adding Using Debug = UnityEngine.Debug; after all the Using directives to include namespaces eg

Using System;
Using System.Diagnostics;
Using UnityEngine;
Using Debug = UnityEngine.Debug;

This will resolve ambiguity between classes

1 Like

FYI if using TimeSpan, you should use duration.TotalMilliseconds for the total elapsed time.

//example

TimeSpan duration = // some duration equal to 2.234 seconds
duration.Milliseconds // returns 234
duration.TotalMilliseconds // returns 2234
4 Likes