Created
June 10, 2013 19:15
-
-
Save JohanLarsson/5751410 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class PerformanceTests | |
| { | |
| [Test] | |
| public void ReadSpeedTest() | |
| { | |
| var excelApp = new Application(); | |
| var workbook = excelApp.Workbooks.Add(); | |
| var sheet = (Worksheet)workbook.Worksheets[1]; | |
| var ns = new[] { 10, 100, 1000, 10000 }; | |
| BatchWrite(sheet, ns.Max()); | |
| var stopwatch = Stopwatch.StartNew(); | |
| foreach (var n in ns) | |
| { | |
| stopwatch.Restart(); | |
| object[] values1 = SeparateReads(sheet, n); | |
| Console.WriteLine("SeparateReads(sheet, " + n + "); took: " + stopwatch.ElapsedMilliseconds + " ms"); | |
| Assert.AreEqual(n, values1.Length); | |
| Assert.AreEqual(0, values1.GetLowerBound(0)); | |
| stopwatch.Restart(); | |
| object[,] values2 = BatchRead(sheet, n); | |
| Console.WriteLine("BatchRead(sheet, " + n + "); took: " + stopwatch.ElapsedMilliseconds + " ms"); | |
| Assert.AreEqual(n, values2.Length); | |
| Assert.AreEqual(1, values2.GetLowerBound(0)); | |
| Assert.AreEqual(1, values2.GetLowerBound(1)); | |
| } | |
| workbook.Close(false); | |
| Marshal.FinalReleaseComObject(excelApp); | |
| } | |
| [Test] | |
| public void WriteSpeedTest() | |
| { | |
| var excelApp = new Application(); | |
| var workbook = excelApp.Workbooks.Add(); | |
| var sheet = (Worksheet)workbook.Worksheets[1]; | |
| var ns = new[] { 10, 100, 1000, 10000 }; | |
| var stopwatch = Stopwatch.StartNew(); | |
| foreach (var n in ns) | |
| { | |
| stopwatch.Restart(); | |
| SeparateWrites(sheet, n); | |
| Console.WriteLine("SeparateWrites(sheet, " + n + "); took: " + stopwatch.ElapsedMilliseconds + " ms"); | |
| stopwatch.Restart(); | |
| BatchWrite(sheet, n); | |
| Console.WriteLine("BatchWrite(sheet, " + n + "); took: " + stopwatch.ElapsedMilliseconds + " ms"); | |
| } | |
| workbook.SaveAs(Path.Combine(@"C:\TEMP", "Test"), ConflictResolution: XlSaveConflictResolution.xlLocalSessionChanges); | |
| workbook.Close(false); | |
| Marshal.FinalReleaseComObject(excelApp); | |
| } | |
| private static void BatchWrite(Worksheet sheet, int n) | |
| { | |
| string[,] values = new string[n, 1]; | |
| var array = Enumerable.Range(1, n).ToArray(); | |
| for (var index = 0; index < array.Length; index++) | |
| { | |
| values[index, 0] = array[index].ToString(); | |
| } | |
| sheet.Range["B1", "B" + n].set_Value(null, values); | |
| } | |
| private static void SeparateWrites(Worksheet sheet, int n) | |
| { | |
| for (int i = 1; i <= n; i++) | |
| { | |
| sheet.Cells[i, 1].Value = i.ToString(); | |
| } | |
| } | |
| private static object[,] BatchRead(Worksheet sheet, int n) | |
| { | |
| var range = sheet.get_Range("A1", "A" + n); | |
| var reads = (object[,])range.Value2; | |
| return reads; | |
| } | |
| private static object[] SeparateReads(Worksheet sheet, int n) | |
| { | |
| var reads = new object[n]; | |
| for (int i = 1; i <= n; i++) | |
| { | |
| reads[i - 1] = sheet.Cells[i, 1].Value2; | |
| } | |
| return reads; | |
| } | |
| private T TimeAndInvoke<T>(Func<T> func, string message) | |
| { | |
| var stopwatch = Stopwatch.StartNew(); | |
| stopwatch.Restart(); | |
| T retval = func(); | |
| Console.WriteLine(message + " took " + stopwatch.GetTimeString()); | |
| return retval; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Results read: | |
| SeparateReads(sheet, 10); took: 77 ms | |
| BatchRead(sheet, 10); took: 83 ms | |
| SeparateReads(sheet, 100); took: 75 ms | |
| BatchRead(sheet, 100); took: 0 ms | |
| SeparateReads(sheet, 1000); took: 766 ms | |
| BatchRead(sheet, 1000); took: 0 ms | |
| SeparateReads(sheet, 10000); took: 8428 ms | |
| BatchRead(sheet, 10000); took: 2 ms | |
| Results write: | |
| SeparateWrites(sheet, 10); took: 23 ms | |
| BatchWrite(sheet, 10); took: 0 ms | |
| SeparateWrites(sheet, 100); took: 96 ms | |
| BatchWrite(sheet, 100); took: 1 ms | |
| SeparateWrites(sheet, 1000); took: 944 ms | |
| BatchWrite(sheet, 1000); took: 2 ms | |
| SeparateWrites(sheet, 10000); took: 10669 ms | |
| BatchWrite(sheet, 10000); took: 13 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment