Я думаю, что проще всего написать консольное приложение Visual Studio. За исключением этого, я использую класс, который я добавил в My Extensions - он не очень точен в том смысле, что он не учитывает собственные накладные расходы, но несколько циклов справки:
using System.Runtime.CompilerServices;
public static class Profiler {
static int depth = 0;
static Dictionary<string, Stopwatch> SWs = new Dictionary<string, Stopwatch>();
static Dictionary<string, int> depths = new Dictionary<string, int>();
static Stack<string> names = new Stack<string>();
static List<string> nameOrder = new List<string>();
static Profiler() {
Init();
}
public static void Init() {
SWs.Clear();
names.Clear();
nameOrder.Clear();
depth = 0;
}
public static void Begin(string name = "",
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0) {
name += $" ({Path.GetFileName(sourceFilePath)}: {memberName}@{sourceLineNumber})";
names.Push(name);
if (!SWs.ContainsKey(name)) {
SWs[name] = new Stopwatch();
depths[name] = depth;
nameOrder.Add(name);
}
SWs[name].Start();
++depth;
}
public static void End() {
var name = names.Pop();
SWs[name].Stop();
--depth;
}
public static void EndBegin(string name = "",
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0) {
End();
Begin(name, memberName, sourceFilePath, sourceLineNumber);
}
public static void Dump() {
nameOrder.Select((name, i) => new {
Key = (new String('\t', depths[name])) + name,
Value = SWs[name].Elapsed
}).Dump("Profile");
}
}