查找方法中的性能瓶颈 [英] Find performance bottleneck in a method

查看:58
本文介绍了查找方法中的性能瓶颈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对代码中的瓶颈有一个特定的问题,我想解决这个问题,但是我想大致了解一下,在将来,我将如何快速找到发生瓶颈的位置而不必重新执行-发明轮子.

I have a specific problem with a bottleneck in my code that I would like to iron out, but I would like to know generally, for the future, how I would go about finding quickly where a bottleneck occurs without having to re-invent the wheel.

这是我方法的代码,但是就像我说的那样,我想知道如何大致确定linq查询花费了多长时间或数据表花费了多长时间等等.

Here is my method's code but like I said, I'd like to know how to generally find out how long the linq query is taking or how long the datatable is taking to build, etc....

public static DataTable GetPivotDataTable(this IQueryable<WbsNumber> table, Months month)
{
      FmToolDataContext dataContext = new FmToolDataContext();

      DataTable dt = new DataTable();

      dt.Columns.Add("id", typeof(string));
      dt.Columns.Add("wbsNumber", typeof(string));
      dt.Columns.Add("wbsTitle", typeof(string));
      dt.Columns.Add("number", typeof(string));
      dt.Columns.Add("vendor", typeof(string));
      dt.Columns.Add("programFund", typeof(string));
      dt.Columns.Add("committedAmount", typeof(decimal));
      dt.Columns.Add("obligatedAmount", typeof(decimal));
      dt.Columns.Add("costedAmount", typeof(decimal));
      dt.Columns.Add("costOverObligationsAmount", typeof(decimal));

      foreach (WbsNumber wbs in table)
      {
        // Get PRs for this Project Detail
        var prAmounts = dataContext.PrAmounts.Where(_pra => _pra.isCurrent && !_pra.requiresAudit
                                                    && _pra.PrDetail.isActive && _pra.PrDetail.WbsNumber == wbs
                                                                                                    && _pra.Import.fiscalYear == (int)HttpContext.Current.Session["FiscalYear"]);



        foreach(PrAmount pra in prAmounts)
        {
            DataRow row = dt.NewRow();

            row["id"] = wbs.id;
            row["wbsNumber"] = wbs.displayString;
            row["wbsTitle"] = wbs.wbsTitlesId != null ? wbs.WbsTitle.name : "(blank)";
            row["number"] = pra.PrDetail.Pr.number;
            row["vendor"] = pra.PrDetail.Pr.GetPrVendorName();
            row["programFund"] = pra.PrDetail.ProgramFund.name;
            row["committedAmount"] = pra.CommittedMonthlyRecord.GetMonth(month);
            row["obligatedAmount"] = pra.ObligatedMonthlyRecord.GetMonth(month);
            row["costedAmount"] = pra.CostedMonthlyRecord.GetMonth(month);
            row["costOverObligationsAmount"] = pra.CostOverObligationsMonthlyRecord.GetMonth(month);

            dt.Rows.Add(row);
        }

            // Get the P-Cards for this Project Detail
        var pCardAmounts = dataContext.PCardAmounts.Where(_pca => _pca.isCurrent && !_pca.requiresAudit
                                                          && _pca.PCardTransaction.isActive && _pca.PCardTransaction.WbsNumber == wbs
                                                                                                                && _pca.Import.fiscalYear == (int)HttpContext.Current.Session["FiscalYear"]);

        foreach (PCardAmount pca in pCardAmounts)
        {
            DataRow row = dt.NewRow();

            row["id"] = wbs.id;
            row["wbsNumber"] = wbs.displayString;
            row["wbsTitle"] = wbs.wbsTitlesId != null ? wbs.WbsTitle.name : "(blank)";
            row["number"] = pca.PCardTransaction.number;
            row["vendor"] = pca.PCardTransaction.bwVendorsId != null ? pca.PCardTransaction.BwVendor.name : "(blank)";
            row["programFund"] = pca.PCardTransaction.ProgramFund.name;
            row["committedAmount"] = pca.CommittedMonthlyRecord.GetMonth(month);
            row["obligatedAmount"] = pca.ObligatedMonthlyRecord.GetMonth(month);
            row["costedAmount"] = pca.CostedMonthlyRecord.GetMonth(month);
            row["costOverObligationsAmount"] = pca.CostOverObligationsMonthlyRecord.GetMonth(month);

            dt.Rows.Add(row);
        }
      }

    return dt;
}

推荐答案

您最好的选择是下载性能分析应用程序,例如

Your best bet is to download a profiling application like Red Gate ANTS Performance Profiler

这将帮助您查明真正的问题(性能问题并不总是表面上看起来的那样).

It'll help you pinpoint the real problem (performance issues aren't always what they seem on the surface).

这篇关于查找方法中的性能瓶颈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆