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?
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));
}
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));
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:
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.
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:
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)
[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;