Profiler

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (C# - Profiler.cs for the iPhone: fixed the formatting)
(Changed name to Profile to remove collision with the unity class of the same name)
Line 13: Line 13:
  
 
==Use==
 
==Use==
Profiler.StartProfile(tag) - Use this to start the profile timer for the specified tag
+
Profile.StartProfile(tag) - Use this to start the profile timer for the specified tag
  
Profiler.EndProfile(tag) - Use this to end the profile of the specified tag
+
Profile.EndProfile(tag) - Use this to end the profile of the specified tag
  
Profiler.Reset() - Remove all profile data and reset the timer, generally not needed
+
Profile.Reset() - Remove all profile data and reset the timer, generally not needed
  
Profiler.PrintResults() - Output all profile data to the console, usually called in OnApplicationQuit() from one location
+
Profile.PrintResults() - Output all profile data to the console, usually called in OnApplicationQuit() from one location
  
  
 
Note: Match up every start tag with an end tag, otherwise the timing data will be wrong
 
Note: Match up every start tag with an end tag, otherwise the timing data will be wrong
  
Note: The profiler can work across scripts and across scenes by starting the tag in one, and ending it in another. (E.G.: Use: Profiler.StartProfile("LoadLevel"); just before Application.LoadLevel(1); and Profiler.EndProfile("LoadLevel"); on a script's function Awake(); in the new scene.)
+
Note: The profiler can work across scripts and across scenes by starting the tag in one, and ending it in another. (E.G.: Use: Profile.StartProfile("LoadLevel"); just before Application.LoadLevel(1); and Profile.EndProfile("LoadLevel"); on a script's function Awake(); in the new scene.)
  
 
==Example - TestProfile.cs==
 
==Example - TestProfile.cs==
Line 35: Line 35:
 
void Start()
 
void Start()
 
{
 
{
Profiler.StartProfile("Start");
+
Profile.StartProfile("Start");
 
}
 
}
 
 
 
void Update ()
 
void Update ()
 
{
 
{
Profiler.StartProfile("Update");
+
Profile.StartProfile("Update");
 
for (int i = 0; i < 100; ++i)
 
for (int i = 0; i < 100; ++i)
 
outputTest += Mathf.Sin(i * Time.time);
 
outputTest += Mathf.Sin(i * Time.time);
Profiler.EndProfile("Update");
+
Profile.EndProfile("Update");
 
}
 
}
 
 
 
void OnApplicationQuit()
 
void OnApplicationQuit()
 
{
 
{
Profiler.EndProfile("Start");
+
Profile.EndProfile("Start");
 
Debug.Log("outputTest is " + outputTest.ToString("F3"));
 
Debug.Log("outputTest is " + outputTest.ToString("F3"));
Profiler.PrintResults();
+
Profile.PrintResults();
 
}
 
}
 
}</csharp>
 
}</csharp>
Line 76: Line 76:
  
  
==C# - Profiler.cs==
+
==C# - Profile.cs==
  
The script should be named Profiler.cs
+
The script should be named Profile.cs
  
  
Line 85: Line 85:
 
using UnityEngine;
 
using UnityEngine;
  
public class Profiler
+
public class Profile
 
{
 
{
 
public struct ProfilePoint
 
public struct ProfilePoint
Line 97: Line 97:
 
private static DateTime startTime = DateTime.UtcNow;
 
private static DateTime startTime = DateTime.UtcNow;
 
 
private Profiler()
+
private Profile()
 
{
 
{
 
}
 
}
Line 141: Line 141:
 
output.Append("\nProfile ");
 
output.Append("\nProfile ");
 
output.Append(pair.Key);
 
output.Append(pair.Key);
output.Append(" took ");
 
output.Append(totalTime.ToString("F5"));
 
output.Append(" seconds to complete over ");
 
output.Append(totalCalls);
 
output.Append(" iteration");
 
if (totalCalls != 1) output.Append("s");
 
output.Append(", averaging ");
 
output.Append((totalTime / totalCalls).ToString("F5"));
 
output.Append(" seconds per call");
 
}
 
output.Append("\n\n============================\n\t\tTotal runtime: ");
 
output.Append(endTime.TotalSeconds.ToString("F3"));
 
output.Append(" seconds\n============================");
 
Debug.Log(output.ToString());
 
}
 
}</csharp>
 
 
== C# - Profiler.cs for the iPhone ==
 
 
The script should be named Profiler.cs
 
(The generic key-value code was changed into non generic (dictionary to hashtable) - which means that a) need to cast when you remove from it  b) need to use DictionaryEntry instead of KeyValuePair.)
 
 
 
 
<csharp>using System.Collections;
 
using System;
 
using UnityEngine;
 
 
public class Profiler
 
{
 
public struct ProfilePoint
 
{
 
public DateTime lastRecorded;
 
public TimeSpan totalTime;
 
public int totalCalls;
 
}
 
 
private static Hashtable profiles = new Hashtable();
 
private static DateTime startTime = DateTime.UtcNow;
 
 
private Profiler()
 
{
 
}
 
 
public static void StartProfile(string tag)
 
{
 
ProfilePoint point;
 
 
if (profiles.ContainsKey(tag))
 
point = (ProfilePoint)profiles[tag];
 
else point = new ProfilePoint();
 
point.lastRecorded = DateTime.UtcNow;
 
profiles[tag] = point;
 
}
 
 
public static void EndProfile(string tag)
 
{
 
if (!profiles.ContainsKey(tag))
 
{
 
Debug.LogError("Can only end profiling for a tag which has already been started (tag was " + tag + ")");
 
return;
 
}
 
ProfilePoint point = (ProfilePoint)profiles[tag];
 
point.totalTime += DateTime.UtcNow - point.lastRecorded;
 
++point.totalCalls;
 
profiles[tag] = point;
 
}
 
 
public static void Reset()
 
{
 
profiles.Clear();
 
startTime = DateTime.UtcNow;
 
}
 
 
public static void PrintResults()
 
{
 
TimeSpan endTime = DateTime.UtcNow - startTime;
 
System.Text.StringBuilder output = new System.Text.StringBuilder();
 
output.Append("============================\n\t\t\t\tProfile results:\n============================\n");
 
foreach(DictionaryEntry pair in profiles)
 
{
 
double totalTime = ((ProfilePoint)pair.Value).totalTime.TotalSeconds;
 
int totalCalls = ((ProfilePoint)pair.Value).totalCalls;
 
if (totalCalls < 1) continue;
 
output.Append("\nProfile ");
 
output.Append((string)pair.Key);
 
 
output.Append(" took ");
 
output.Append(" took ");
 
output.Append(totalTime.ToString("F5"));
 
output.Append(totalTime.ToString("F5"));

Revision as of 17:28, 20 March 2011

Author: Michael Garforth


Contents

Overview

This C# class gives simple access to simple profiling of code

The script can be placed in an early compilation pass (e.g. Plugins directory) so that it can be used from any language

It has no dependencies besides .NET and Unity


Use

Profile.StartProfile(tag) - Use this to start the profile timer for the specified tag

Profile.EndProfile(tag) - Use this to end the profile of the specified tag

Profile.Reset() - Remove all profile data and reset the timer, generally not needed

Profile.PrintResults() - Output all profile data to the console, usually called in OnApplicationQuit() from one location


Note: Match up every start tag with an end tag, otherwise the timing data will be wrong

Note: The profiler can work across scripts and across scenes by starting the tag in one, and ending it in another. (E.G.: Use: Profile.StartProfile("LoadLevel"); just before Application.LoadLevel(1); and Profile.EndProfile("LoadLevel"); on a script's function Awake(); in the new scene.)

Example - TestProfile.cs

<csharp>using UnityEngine;

public class TestProfile : MonoBehaviour { private float outputTest = 0;

void Start() { Profile.StartProfile("Start"); }

void Update () { Profile.StartProfile("Update"); for (int i = 0; i < 100; ++i) outputTest += Mathf.Sin(i * Time.time); Profile.EndProfile("Update"); }

void OnApplicationQuit() { Profile.EndProfile("Start"); Debug.Log("outputTest is " + outputTest.ToString("F3")); Profile.PrintResults(); } }</csharp>


Output:

============================

Profile results:

============================


Profile Update took 0.03267 seconds to complete over 1568 iterations, averaging 0.00002 seconds per call

Profile Start took 7.56886 seconds to complete over 1 iteration, averaging 7.56886 seconds per call


============================

Total runtime: 7.580 seconds

============================


C# - Profile.cs

The script should be named Profile.cs


<csharp>using System.Collections.Generic; using System; using UnityEngine;

public class Profile { public struct ProfilePoint { public DateTime lastRecorded; public TimeSpan totalTime; public int totalCalls; }

private static Dictionary<string, ProfilePoint> profiles = new Dictionary<string, ProfilePoint>(); private static DateTime startTime = DateTime.UtcNow;

private Profile() { }

public static void StartProfile(string tag) { ProfilePoint point;

profiles.TryGetValue(tag, out point); point.lastRecorded = DateTime.UtcNow; profiles[tag] = point; }

public static void EndProfile(string tag) { if (!profiles.ContainsKey(tag)) { Debug.LogError("Can only end profiling for a tag which has already been started (tag was " + tag + ")"); return; } ProfilePoint point = profiles[tag]; point.totalTime += DateTime.UtcNow - point.lastRecorded; ++point.totalCalls; profiles[tag] = point; }

public static void Reset() { profiles.Clear(); startTime = DateTime.UtcNow; }

public static void PrintResults() { TimeSpan endTime = DateTime.UtcNow - startTime; System.Text.StringBuilder output = new System.Text.StringBuilder(); output.Append("============================\n\t\t\t\tProfile results:\n============================\n"); foreach(KeyValuePair<string, ProfilePoint> pair in profiles) { double totalTime = pair.Value.totalTime.TotalSeconds; int totalCalls = pair.Value.totalCalls; if (totalCalls < 1) continue; output.Append("\nProfile "); output.Append(pair.Key); output.Append(" took "); output.Append(totalTime.ToString("F5")); output.Append(" seconds to complete over "); output.Append(totalCalls); output.Append(" iteration"); if (totalCalls != 1) output.Append("s"); output.Append(", averaging "); output.Append((totalTime / totalCalls).ToString("F5")); output.Append(" seconds per call"); } output.Append("\n\n============================\n\t\tTotal runtime: "); output.Append(endTime.TotalSeconds.ToString("F3")); output.Append(" seconds\n============================"); Debug.Log(output.ToString()); } }</csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox