问题
Environment - Windows 10, Visual Studio 2017, netcore2.0, Debug build.
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.
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.
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.
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.
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.");
}
}
}
}
回答1:
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.
I advice you to use the .net core 2 compatible pre-release Environment
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.
回答2:
I had the same issue, it was because I stored character values in cells : worksheet.Cells[i,j].Value = 'A'; It is working BUT when saving it produces a conversion cast exception that slows a lot the VS debugger. Just store a string! worksheet.Cells[i,j].Value = "A";
来源:https://stackoverflow.com/questions/47822689/very-slow-operation-of-epplus-save-saveas-etc-methods-when-debugging