我想确定在哪里优化LINQPad查询中的代码。我怎么能这么做?
请注意,我不是在问如何分析LINQ查询;只是在LINQPad 'query‘文件(普通LINQPad文件)中使用常规(LINQPad)代码。
发布于 2018-05-02 21:45:36
我认为最简单的方法是编写Visual控制台应用程序。除此之外,我使用我在扩展中添加的类--它并不是非常准确,因为它没有很好地说明自己的开销,而是通过帮助实现了多个循环:
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");
}
}https://stackoverflow.com/questions/50143200
复制相似问题