using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace bbiwarg.Utility { class Timer { private static Dictionary stopwatches = new Dictionary(); private static Dictionary currentTimes = new Dictionary(); private static Dictionary minTimes = new Dictionary(); private static Dictionary maxTimes = new Dictionary(); private static Dictionary sumTimes = new Dictionary(); private static Dictionary numTimes = new Dictionary(); public static void start(String name) { if (!stopwatches.ContainsKey(name)) { stopwatches.Add(name, new Stopwatch()); minTimes.Add(name, int.MaxValue); maxTimes.Add(name, 0); sumTimes.Add(name, 0); numTimes.Add(name, 0); currentTimes.Add(name, 0); } stopwatches[name].Restart(); } public static void stop(String name) { stopwatches[name].Stop(); double time = Math.Round((double)stopwatches[name].ElapsedTicks / (double)Stopwatch.Frequency * 1000.0, 2); if (time < minTimes[name]) minTimes[name] = time; if (time > maxTimes[name]) maxTimes[name] = time; sumTimes[name] += time; numTimes[name]++; currentTimes[name] = time; } public static void output(String name) { Logger.log(String.Format("name:{0}\tavg:{1:00.00}\tcurrent:{2:00.00}", name, sumTimes[name] / Math.Max(numTimes[name], 1), currentTimes[name]), LogSubject.Timer); } public static void outputAll() { Logger.clear(LogSubject.Timer); Logger.log("---TIMERS-START---", LogSubject.Timer); foreach (String name in stopwatches.Keys) { output(name); } Logger.log("---TIMERS-END---", LogSubject.Timer); } } }