profiling - 如何分析 LINQPad 查询?
问题描述
我想确定在 LINQPad 查询中优化代码的位置。我怎样才能做到这一点?
请注意,我不是在问如何分析LINQ 查询;只是 LINQPad“查询”文件(常规 LINQPad 文件)中的常规(C#)代码。
解决方案
我认为最简单的方法是编写一个 Visual Studio 控制台应用程序。除此之外,我使用了我添加到我的扩展中的一个类——它并不是非常准确,因为它不能很好地考虑到它自己的开销,而是通过帮助进行多个循环:
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");
}
}
推荐阅读
- r - R tidyverse,将数据重塑为每个主题一行,但有几列受到影响
- verilog - 如何在 Verilog 中实例化具有 reg 端口的模块?
- haskell - 学习 Haskell 并想做一个反向波兰符号评估器
- node.js - 'graphql' 不是内部或外部命令、可运行程序或批处理文件。在部署棱镜时
- php - 在登录表单下拉菜单中列出已经注册的用户 - 终极会员插件
- python - 支持 GPU 的 Levenberg-Marquardt 算法
- c# - System.Text.Json 反序列化来自 API 调用的嵌套对象 - 数据包装在父 JSON 属性中
- powerbi - 用于动态模型评估的 DAX 度量
- javascript - 尝试使用 JavaScript 将 HTML 表单数据发送到 API
- javascript - 根据某些情况在另一个函数内部运行一个函数