首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何分析LINQPad查询?

如何分析LINQPad查询?
EN

Stack Overflow用户
提问于 2018-05-02 20:37:47
回答 1查看 448关注 0票数 2

我想确定在哪里优化LINQPad查询中的代码。我怎么能这么做?

请注意,我不是在问如何分析LINQ查询;只是在LINQPad 'query‘文件(普通LINQPad文件)中使用常规(LINQPad)代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-02 21:45:36

我认为最简单的方法是编写Visual控制台应用程序。除此之外,我使用我在扩展中添加的类--它并不是非常准确,因为它没有很好地说明自己的开销,而是通过帮助实现了多个循环:

代码语言:javascript
复制
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");
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50143200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档