【问题标题】:How to reduce the program execution time and increase the overall performance? [closed]如何减少程序执行时间,提高整体性能? [关闭]
【发布时间】:2014-02-03 12:45:49
【问题描述】:

我创建了一个 Windows 应用程序,它处理文件夹中的一组 excel 文件,从每个文件中查找一些选择性信息。稍后,它将所有这些信息存储到一个新的 Excel 表中。它是一种报告生成工具。我很难尝试减少此应用程序的总执行时间。目前,完成一个成功执行周期总共需要大约 45-50 秒。代码类似这样:

附:由于限制政策,我无法分享实际代码。但是下面显示的代码足以提供一些解决方案。

    #region master_tracking_sheet
    public string[] xlsfiles;
    public string[] personList = new string[35];
    public string[,] excelSheet_det = new string[35, 6];
    private void buttonBrowseFolder_Click(object sender, EventArgs e)
    {
        if (folder_browse_excelSheet.ShowDialog() == DialogResult.OK)
        {
            this.txt_DirPath.Text = folder_browse_excelSheet.SelectedPath;
            gotFile = true;
        }

    }
    private void btnCreateMasterSheet_Click(object sender, EventArgs e)
    {
        try
        {
            if (gotFile == false)
            {
                MessageBox.Show("Please select a folder first!");
            }
            else
            {
                var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
                {
                    ".xls",
                    ".xlsx",
                };
                userNotifier = false;
                var baseDir = txt_DirPath.Text;
                var file_count = Directory.EnumerateFiles(baseDir).Count(filename => extensions.Contains(Path.GetExtension(filename)));
                Excel.Application application = new Excel.Application();
                xlsfiles = Directory.GetFiles(folder_browse_excelSheet.SelectedPath, "*.xls");
                for (int j = 1; j <= file_count; j++) //Generating list of persons whose excelSheets are there
                {
                    csvFilePath = System.IO.Path.GetFileNameWithoutExtension(xlsfiles[j - 1]);
                    personList[j - 1] = csvFilePath.ToString();
                }
                bool check_proceed = checkpersonSheetExist(personList);//Check for any missing excelSheet file as per the given person list
                if (check_proceed == false)
                {
                    application.Quit();
                    killApplication(true);
                }
                else
                {
                    for (int i = 1; i <= file_count; i++)
                    {
                        Excel.Workbook temp_wb;
                        excelSheet_det[i - 1, 0] = personList[i - 1].ToString(); //Adding person name as first entry
                        string excelFileName = xlsfiles[i - 1];
                        excelFilePath = excelFileName; //reassigning the excelFilePath to make an entry in excelSheet Log.txt
                        findWordInFile("Non Working", excelFileName); //Calling the method to identify Non-working entry if any
                        //The above method sets flag value to 'y' if true else the flag value remains to 'n'
                        temp_wb = application.Workbooks.Open(excelFileName);
                        Excel.Worksheet worksheet;
                        worksheet = temp_wb.Sheets[1];
                        var cell_val = 0.0;
                        var cell_valA = 0.0;
                        if ((worksheet.Cells[5, 25] as Excel.Range).Value == null || (worksheet.Cells[5, 25] as Excel.Range).Value == 0)
                            cell_val = 0;
                        else
                            cell_val = (double)(worksheet.Cells[5, 25] as Excel.Range).Value;
                        if ((worksheet.Cells[4, 25] as Excel.Range).Value == null || (worksheet.Cells[4, 25] as Excel.Range).Value==0)
                            cell_valA = 0;
                        else
                            cell_valA = (double)(worksheet.Cells[4, 25] as Excel.Range).Value;
                        if (cell_val == 0)
                        {
                            excelSheet_det[i - 1, 1] = cell_valA.ToString(); //Adding Normal Hours
                        }
                        else
                        {
                            excelSheet_det[i - 1, 1] = cell_val.ToString(); //Adding Normal Hours
                        }
                        if (flag == 'y') //if non-working non billable entry found
                        {
                            var cell_val1 = 0.0;
                            if ((worksheet.Cells[14, 25] as Excel.Range).Value == null || (worksheet.Cells[14, 25] as Excel.Range).Value == 0.0)
                                cell_val1 = 0;
                            else
                                cell_val1 = (double)(worksheet.Cells[14, 25] as Excel.Range).Value;
                            excelSheet_det[i - 1, 3] = cell_val1.ToString();//Adding Non-working Hours
                            excelSheet_det[i - 1, 2] = "0"; //Setting working hours to zero
                        }
                        else //if non-working non billable entry NOT found
                        {
                            var cell_val2 = 0.0;
                            if ((worksheet.Cells[14, 25] as Excel.Range).Value == null || (worksheet.Cells[14, 25] as Excel.Range).Value == 0)
                                cell_val2 = 0;
                            else
                                cell_val2 = (double)(worksheet.Cells[14, 25] as Excel.Range).Value;
                            excelSheet_det[i - 1, 2] = cell_val2.ToString();//Adding working Hours
                            excelSheet_det[i - 1, 3] = "0"; //Setting non-working hours to zero
                        }
                        var cell_val3 = 0.0;
                        var cell_val3_1 = 0.0;
                        var cell_val3_2 = 0.0;
                        if ((worksheet.Cells[15, 25] as Excel.Range).Value == null || (worksheet.Cells[15, 25] as Excel.Range).Value == 0)
                            cell_val3 = 0;
                        else
                            cell_val3 = (double)(worksheet.Cells[15, 25] as Excel.Range).Value;
                        if ((worksheet.Cells[13, 25] as Excel.Range).Value == null || (worksheet.Cells[13, 25] as Excel.Range).Value == 0)
                            cell_val3_1 = 0;
                        else
                            cell_val3_1 = (double)(worksheet.Cells[13, 25] as Excel.Range).Value;
                        if ((worksheet.Cells[12, 25] as Excel.Range).Value == null || (worksheet.Cells[12, 25] as Excel.Range).Value == 0)
                            cell_val3_2 = 0;
                        else
                            cell_val3_2 = (double)(worksheet.Cells[12, 25] as Excel.Range).Value;
                        if (cell_val3 == 0 && cell_val3_1 == 0)
                        {
                            excelSheet_det[i - 1, 4] = cell_val3_2.ToString();
                        }
                        else if (cell_val3_1 == 0 && cell_val3_2 == 0)
                        {
                            excelSheet_det[i - 1, 4] = cell_val3.ToString();
                        }
                        else
                        {
                            excelSheet_det[i - 1, 4] = cell_val3_1.ToString();
                        }
                        var cell_val4 = DateTime.MinValue;
                        if ((worksheet.Cells[3, 24] as Excel.Range).Value == null)
                            cell_val4 = DateTime.MinValue;
                        else
                            cell_val4 = (worksheet.Cells[3, 24] as Excel.Range).Value;
                        excelSheet_det[i - 1, 5] = cell_val4.ToString();//copy last date of excelSheet entry
                        Marshal.ReleaseComObject(temp_wb);
                    }
                    application.Workbooks.Close();
                    application.Quit();
                }
                /*Writing the array contents into the excel master sheet*/
                string masterSheetPath = folder_browse_excelSheet.SelectedPath + "\\Master Spreadsheet\\" + "MasterSheet.xls";
                if (!Directory.Exists(folder_browse_excelSheet.SelectedPath +
                     "\\masterSheet"))
                {
                    Directory.CreateDirectory(folder_browse_excelSheet.SelectedPath + "\\Master Spreadsheet");
                }
                if (!File.Exists(masterSheetPath))
                {
                    FileStream fs = File.Create(masterSheetPath);
                    fs.Close();
                }
                Excel.Application excel = new Excel.Application();
                Excel.Workbook workBook = excel.Workbooks.Open(masterSheetPath);
                Excel.Worksheet sheet = workBook.ActiveSheet;
                int numRows = sheet.UsedRange.Rows.Count;
                //MessageBox.Show(numRows.ToString());
                //Check for any duplicate entries in the mastersheet
                string masterDupErrDisplay = string.Empty;
                if (numRows > 1)
                {
                    for (int i1 = 0; i1 < xlsfiles.Length; i1++)
                    {
                        for (int ch = 0; ch < numRows; ch++)
                        {
                            if (excelSheet_det[i1, 5] == (((sheet.Cells[ch + 1, 6] as Excel.Range).Value)).ToString() && excelSheet_det[i1, 0] == ((string)(sheet.Cells[ch + 1, 1] as Excel.Range).Value))
                            {
                                if (!masterDupErrDisplay.Contains(excelSheet_det[i1, 0].ToString()))
                                {
                                    masterDupErrDisplay += string.Join(Environment.NewLine, excelSheet_det[i1, 0].ToString());
                                    masterDupErrDisplay += string.Join(Environment.NewLine, Environment.NewLine);
                                }
                            }

                        }
                    }
                    if (masterDupErrDisplay != null)
                    {
                        if (MessageBox.Show("Duplicate entries have been found for these people " + Environment.NewLine + masterDupErrDisplay + Environment.NewLine + "Do you wish to Continue?", "Duplicate Mastersheet entry Error", MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            excel.Quit();
                            killApplication(true);
                        }
                    }
                }
                sheet.Cells[1, 1] = "Name";
                sheet.Cells[1, 2] = "Normal Hours";
                sheet.Cells[1, 3] = "Working Hours";
                sheet.Cells[1, 4] = "Not working Hours";
                sheet.Cells[1, 5] = "Total Hours";
                sheet.Cells[1, 6] = "excelSheet Stamp";
                for (int i = 0; i < xlsfiles.Length; i++)
                {
                    sheet.Cells[numRows + 1 + i, 1] = excelSheet_det[i, 0];
                    sheet.Cells[numRows + 1 + i, 2] = excelSheet_det[i, 1];
                    sheet.Cells[numRows + 1 + i, 3] = excelSheet_det[i, 2];
                    sheet.Cells[numRows + 1 + i, 4] = excelSheet_det[i, 3];
                    sheet.Cells[numRows + 1 + i, 5] = excelSheet_det[i, 4];
                    sheet.Cells[numRows + 1 + i, 6] = excelSheet_det[i, 5];
                }
                workBook.RefreshAll();
                excel.Calculate();
                workBook.Save();
                workBook.Close(true);
                excel.Workbooks.Close();
                excel.Quit();
                application.Quit();
                MessageBox.Show("Master track sheet is successfully updated!", "Process Successful");
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion master_tracking_sheet
    public bool checkpersonSheetExist(string[] assocName)
    {
        try
        {
            string[,] personListSupply = new string[35, 2];
            int counter = 0;
            string errorDisplay = string.Empty;
            string line;
            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(folder_browse_excelSheet.SelectedPath + "\\team_members.txt");
            while ((line = file.ReadLine()) != null)
            {
                personListSupply[counter, 0] = line.ToString();
                personListSupply[counter, 1] = "n";
                counter++;
            }
            file.Close();
            int c = 0;
            while (assocName[c] != null)
            {
                c++;
            }
            for (int i = 0; i < counter; i++)
            {
                for (int k = 0; k < c; k++)
                {
                    if (personListSupply[i, 0] == assocName[k])
                    {
                        personListSupply[i, 1] = "y";
                        continue;
                    }

                }
                if (personListSupply[i, 1] == "n")
                {
                    errorDisplay += string.Join(Environment.NewLine, personListSupply[i, 0].ToString());
                    errorDisplay += string.Join(Environment.NewLine, Environment.NewLine);

                }
            }
            if (MessageBox.Show("The excel files are missing for" + Environment.NewLine + errorDisplay + Environment.NewLine + "Do you wish to Continue?", "File Missing Error", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

【问题讨论】:

  • 是什么让您认为这可以更快地完成?您是否进行了任何分析以发现大部分时间都花在了哪里? excel文件有多大?老实说,我想知道您是否会更好地使用 VBA - 您至少会“留在 Excel 空间中”,而不是对将 Excel 连接到 C# 的代码进行大量昂贵的调用。
  • @Floris:我发现大部分时间都花在最里面的 for 循环中,它从当前的 excel 文件中检索值。 excel 文件的大小约为 80 KB。我对这个winforms概念很陌生。我没有考虑过使用 VBA。你到底在说什么:“你至少会“留在 Excel 空间”而不是打很多昂贵的电话”?能详细点吗?
  • 您在代码中至少创建了两个额外的 Excel 实例 - 根据您的机器,这可能会很慢。看起来你可能只需要一些常规的 excel 编码(可能只是一个数据透视表)——如果不逐行挑选代码,这有点难以分辨。但我坚信 C# 解决方案不是将数据从一个 Excel 电子表格导入另一个电子表格的方法。假设你只能使用 Excel 来解决这个问题 - 你会很高兴你做到了。
  • @JeremyThompson:这并不是完全重复的情况。如果您仔细阅读代码,您会注意到需要进行一些修改才能使此代码运行得更快。我正在寻找这样的修改。

标签: c# winforms visual-studio-2010 performance excel


【解决方案1】:

我会使用一些商业或开源 .NET Excel 库,例如 SpreadsheetLight,它允许直接创建 xls 和 xslx 文件,而无需基于 UI 的 Excel 应用程序。

你也应该看看OpenXML

【讨论】:

  • 我希望我可以使用一些商业图书馆。但是,我希望在不使用任何现有第三方库的情况下使其成为可能。
【解决方案2】:

你可以做很多事情

1.你可以提高你的代码质量(添加记录器语句并找出哪个函数需要时间调试它,如果可能的话改进它)

2. 使用比“重但易于使用”的数据结构和函数更少的重数据结构。 (这确实提高了性能)

3.删除不需要的代码并保存不需要的 I/O 操作(每次从文件中读取,而不是获取一次并对其执行操作。这也提高了一点性能)

4.如果可能使用多线程,我不是 C# 人,但如果可能的话使用线程。这肯定会显着提高性能(如果在您的情况下可以进行并发文件操作并且如果您的 CPU 功率很高,那么试试这个)

【讨论】:

  • 感谢您的回复。好吧,这个应用程序将在不同类型的机器上使用,所以即使我选择多线程,它也可能在硬件配置较低的机器上造成问题。不管它肯定会提高我的代码的性能。其次,在每次迭代中,都会加载一个新文件。所以,据我所知,没有这种不需要的 I/O 操作。如果你能纠正我,我会很高兴。
【解决方案3】:

我可以想出一些方法来加快速度,但我推荐给您的链接Excel Interop - Efficiency and performance 是最好的链接之一。

如果你真的想要速度,你可以:

  • 使用 OpenXML 避免互操作开销
  • 不声明两个 Excel 实例和/或多个工作簿变量
  • 在处理时关闭 Excel 更新 UI

编辑:尝试关闭 ScreenUpdating 并将计算设置为手动(以避免任何后续 Excel 计算 - 如果有任何公式引用您正在更新的单元格):

private static XlCalculation xlCalculation = XlCalculation.xlCalculationAutomatic;
static public void TurnOffApplicationSettings(Excel.Application xlApp)
{
    xlApp.ScreenUpdating = false;
    xlApp.DisplayAlerts = false;
    xlCalculation = xlApp.Calculation; //Record the current Calculation Mode
    xlApp.Calculation = XlCalculation.xlCalculationManual;
    xlApp.UserControl = false;
    xlApp.EnableEvents = false;
}

然后运行您的长时间操作并将设置恢复为 true 并将计算恢复为自动(或更准确地说是之前设置的值):

static public void TurnOnApplicationSettings(Excel.Application xlApp)
{
    xlApp.ScreenUpdating = true;
    xlApp.DisplayAlerts = true;
    xlApp.Calculation = xlCalculation;
    xlApp.UserControl = true;
    xlApp.EnableEvents = true;
}

【讨论】:

  • 确实Excel Interop - Efficiency and performance 是一个很好的解决方案。我目前正在我现有的代码上实施。而且,由于限制政策,我无法使用某些第三方库,例如 OpenXML 或 SpreadSheetLight。您的观点到底指的是哪里:“•在处理时关闭 Excel 更新 UI”?我在上面的代码中实际上可以在哪里使用它?
  • 希望我的 edit 使第四次速度优化更易于理解和实施。弹出调用以在子例程开始时关闭 Off 并在子例程结束时将其返回 On
  • 谢谢!我明白你的意思了!
猜你喜欢
  • 2012-01-16
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 2021-07-09
  • 1970-01-01
相关资源
最近更新 更多