Advertisement
Guest User

Untitled

a guest
Oct 30th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. #pragma warning disable 0162
  2. #define PROFILE
  3. using System;
  4.  
  5. namespace Pathfinding
  6. {
  7.    
  8.    
  9.     class Profile {
  10.         const bool PROFILE_MEM = true;
  11.        
  12.         public string name;
  13.         System.Diagnostics.Stopwatch w;
  14.         int counter = 0;
  15.         long mem = 0;
  16.         long smem = 0;
  17.        
  18.         int control = 1 << 30;
  19.         bool dontCountFirst = false;
  20.        
  21.         public int ControlValue () {
  22.             return control;
  23.         }
  24.        
  25.         public Profile (string name) {
  26.             this.name = name;
  27.             w = new System.Diagnostics.Stopwatch();
  28.         }
  29.        
  30.         [System.Diagnostics.ConditionalAttribute("PROFILE")]
  31.         public void Start () {
  32.             if (PROFILE_MEM) {
  33.                 smem = System.GC.GetTotalMemory(false);
  34.             }
  35.             if (dontCountFirst && counter == 1) return;
  36.             w.Start();
  37.         }
  38.        
  39.         [System.Diagnostics.ConditionalAttribute("PROFILE")]
  40.         public void Stop () {
  41.             counter++;
  42.             if (dontCountFirst && counter == 1) return;
  43.            
  44.             w.Stop();
  45.             if (PROFILE_MEM) {
  46.                 mem += System.GC.GetTotalMemory(false)-smem;
  47.             }
  48.            
  49.         }
  50.        
  51.         [System.Diagnostics.ConditionalAttribute("PROFILE")]
  52.         /** Log using Debug.Log */
  53.         public void Log () {
  54.             UnityEngine.Debug.Log (ToString());
  55.         }
  56.        
  57.         [System.Diagnostics.ConditionalAttribute("PROFILE")]
  58.         /** Log using System.Console */
  59.         public void ConsoleLog () {
  60.             System.Console.WriteLine (ToString());
  61.         }
  62.        
  63.         [System.Diagnostics.ConditionalAttribute("PROFILE")]
  64.         public void Stop (int control) {
  65.             counter++;
  66.             if (dontCountFirst && counter == 1) return;
  67.            
  68.             w.Stop();
  69.             if (PROFILE_MEM) {
  70.                 mem += System.GC.GetTotalMemory(false)-smem;
  71.             }
  72.            
  73.             if (this.control == 1 << 30) this.control = control;
  74.             else if (this.control != control) throw new System.Exception("Control numbers do not match " + this.control + " != " + control);
  75.         }
  76.        
  77.         [System.Diagnostics.ConditionalAttribute("PROFILE")]
  78.         public void Control (Profile other) {
  79.             if (ControlValue() != other.ControlValue()) {
  80.                 throw new System.Exception("Control numbers do not match ("+name + " " + other.name + ") " + this.ControlValue() + " != " + other.ControlValue());
  81.             }
  82.         }
  83.        
  84.         public override string ToString () {
  85.             string s = name + " " + w.Elapsed.TotalMilliseconds.ToString("0.0 ms") + " avg: " + (w.Elapsed.TotalMilliseconds/counter).ToString("0.00 ms");
  86.             if (PROFILE_MEM) {
  87.                 s += " avg mem: " + (mem/(1.0*counter)).ToString("0 bytes");
  88.             }
  89.             return s;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement