调试时,Epplus Save(),SaveAs()等方法的运行非常缓慢 [英] Very slow operation of Epplus Save(), SaveAs(), etc. methods when debugging

查看:543
本文介绍了调试时,Epplus Save(),SaveAs()等方法的运行非常缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境-Windows 10,Visual Studio 2017,netcore2.0,调试版本。

Environment - Windows 10, Visual Studio 2017, netcore2.0, Debug build.

在调试时使用Epplus 4.1.1保存Excel文件时,保存操作比我期望的要花费更长的时间。

When saving an Excel file using Epplus 4.1.1 while debugging, the save operation takes a much longer amount of time than I would expect.

使用Debug->开始调试有一些时间

Here's some timing when ran with Debug -> Start Debugging

Saving 100 rows in 2065ms.
Saving 200 rows in 2050ms.
Saving 400 rows in 4003ms.
Saving 800 rows in 11360ms.
Saving 1600 rows in 18377ms.
Saving 3200 rows in 34139ms.

与运行Debug时相同->不调试就开始

And the same when ran with Debug -> Start Without Debugging

Saving 100 rows in 401ms.
Saving 200 rows in 49ms.
Saving 400 rows in 28ms.
Saving 800 rows in 58ms.
Saving 1600 rows in 94ms.
Saving 3200 rows in 198ms.

在面向.NET Framework 4.6.1时,调试时的结果要好得多-

When targeting .NET Framework 4.6.1, results while debugging are vastly better -

Saving 100 rows in 215ms.
Saving 200 rows in 26ms.
Saving 400 rows in 42ms.
Saving 800 rows in 78ms.
Saving 1600 rows in 146ms.
Saving 3200 rows in 279ms.

我显然知道糖蜜的调试速度很慢,但这是极少量的数据。在针对.NET Core 2.0进行调试时,有什么我可以做的事情来加快速度?

I obviously know that debugging is molasses slow, but this is a miniscule amount of data. Is there anything I can do to poke this along to go faster when debugging targeting .NET Core 2.0?

-

这里是产生上述输出的程序-

Here is the program that produced the above output -

namespace ConsoleApp9
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Linq;
    using System.Diagnostics;

    using OfficeOpenXml;

    public class Foo
    {
        public string Name { get; set; }
        public int Number { get; set; }
        public char? Character { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            IEnumerable<Foo> RandomFoo()
            {
                var random = new Random();
                const string chars = "ABCDEFGHIJKLH";
                while (true)
                {
                    yield return new Foo
                    {
                        Character = random.NextDouble() < .1 ? null : (char?) 'A',
                        Number = random.Next(0, 10),
                        Name = new string(Enumerable.Repeat(chars, 8)
                            .Select(x => x[random.Next(x.Length)])
                            .ToArray()
                        )
                    };
                }
            }

            for (var i = 100; i <= 1000000; i *= 2)
            {
                var sw = Stopwatch.StartNew();

                var stream = new MemoryStream();
                using (var package = new ExcelPackage(stream))
                {
                    var sheet = package.Workbook.Worksheets.Add("Sheet");
                    var foos = RandomFoo().Take(i);
                    sheet.Cells["A1"].LoadFromCollection(foos);

                    package.Save(); // very long operation here
                    stream.Seek(0, SeekOrigin.Begin);
                }

                var file = new FileInfo("test.xlsx");
                using (var fileStream = file.Create())
                {
                    stream.CopyTo(fileStream);
                }

                Console.WriteLine($"Saving {i} rows in {sw.ElapsedMilliseconds}ms.");
            }
        }
    }
}


推荐答案

EPPLUS v4.4.1不能完全与.net core 2兼容,但是我得到的数字仍然不同于您(逻辑上的数字)。

EPPLUS v4.4.1 is not fully .net core 2 compatible, still I got numbers different than yours (logical ones).

Saving 100 rows in 360ms.
Saving 200 rows in 23ms.
Saving 400 rows in 48ms.
Saving 800 rows in 84ms.
Saving 1600 rows in 183ms.
Saving 3200 rows in 317ms.
Saving 6400 rows in 592ms.
Saving 12800 rows in 1157ms.
Saving 25600 rows in 2352ms.
Saving 51200 rows in 4848ms.
Saving 102400 rows in 9613ms.
Saving 204800 rows in 18917ms.

我建议您使用.net core 2兼容的预发布
环境

I advice you to use the .net core 2 compatible pre-release Environment

以下是我在Windows 10,Visual Studio 2017,netcore2.0,调试版本中使用EPPLUS v4.5.0.1-beta所获得的数字。

Here are the numbers I got with EPPLUS v4.5.0.1-beta in Windows 10, Visual Studio 2017, netcore2.0, Debug build.

Saving 100 rows in 585ms.
Saving 200 rows in 114ms.
Saving 400 rows in 125ms.
Saving 800 rows in 159ms.
Saving 1600 rows in 233ms.
Saving 3200 rows in 360ms.
Saving 6400 rows in 655ms.
Saving 12800 rows in 1251ms.
Saving 25600 rows in 2422ms.
Saving 51200 rows in 4840ms.
Saving 102400 rows in 9789ms.
Saving 204800 rows in 19094ms.
Saving 409600 rows in 38093ms.

这篇关于调试时,Epplus Save(),SaveAs()等方法的运行非常缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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