Performance, performance and performance

by clearoffice 27. October 2009 03:55

One of the great benefits of using .NET everywhere is performance.

For example, consider the following code snippet.

        private static void TestMicrosoftExcel()
        {
            var xl = new ApplicationClass();
            Workbook wb = xl.Workbooks.Add(Missing.Value);
            var ws = (Worksheet) wb.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            for (int i = 1; i < 500; i++)
                for (int j = 1; j < 100; j++)
                    ws.Cells[i, j] = 123;
            wb.SaveAs(@"c:\temp\xls1.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                      XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                      Missing.Value);
        }

It creates a simple spreadsheet with 500 * 100 = 50000 values.

Problem is, it takes 2 minutes to run.

Let's write the equivalent with Clear Spreadsheet:

        private static void TestClearSpreadsheet()
        {
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create();
            var worksheet = (Office.Spreadsheet.Worksheet) spreadsheetDocument.Workbook.Sheets[0];
            for (int i = 1; i < 500; i++)
                for (int j = 1; j < 100; j++)
                    worksheet[i, j].Value = 123;
            spreadsheetDocument.SaveAs(@"c:\temp\xls2.xlsx");
        }

Not only is it somewhat more elegant, it is also faster: 4 seconds.

Recap:

Microsoft Excel: 120 seconds

Clear Spreadsheet: 4 seconds

Try it for yourself!

Tags: