Skip to content

Instantly share code, notes, and snippets.

@govert
Last active July 2, 2020 20:45
Show Gist options
  • Select an option

  • Save govert/8fc38cf335462a1d9baef8a61f2bba6d to your computer and use it in GitHub Desktop.

Select an option

Save govert/8fc38cf335462a1d9baef8a61f2bba6d to your computer and use it in GitHub Desktop.

Revisions

  1. govert revised this gist Jul 2, 2020. 1 changed file with 19 additions and 10 deletions.
    29 changes: 19 additions & 10 deletions SetValuePerf.cs
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,41 @@
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ExcelDna.Integration;
    using static ExcelDna.Integration.XlCall;

    namespace SetValuePerf
    {
    public static class Macros
    {

    [ExcelCommand(ShortCut = "^D")] // Ctrl + Shift + D
    public static void DumpValues()
    {
    var numWritesInput = Excel(xlfInput, "Number of writes:", 1, "SetValuePerf Test", 20000);
    if (numWritesInput is bool)
    return;
    var numWrites = Convert.ToInt32(numWritesInput);

    var sw = Stopwatch.StartNew();
    for (int i = 0; i < 20000; i++)
    for (int i = 0; i < numWrites; i++)
    {
    var r = new ExcelReference(i, i, 0, 1);
    r.SetValue(new object[,] { { $"Info - {i}", DateTime.Now } });
    }
    new ExcelReference(0, 4).SetValue("ElapsedMs:");
    new ExcelReference(0, 5).SetValue(sw.ElapsedMilliseconds);
    sw.Stop();
    new ExcelReference(0, 4).SetValue("Writes / ElapsedMs / Writes per ms:");
    new ExcelReference(0, 0, 5, 7).SetValue(new object[,] { { numWrites, sw.ElapsedMilliseconds, (double)numWrites / sw.ElapsedMilliseconds } });
    }


    [ExcelCommand(ShortCut = "^E")] // Ctrl + Shift + E
    public static void DumpValuesEverywhere()
    {
    var numWritesInput = Excel(xlfInput, "Number of writes:", 1, "SetValuePerf Test", 20000);
    if (numWritesInput is bool)
    return;
    var numWrites = Convert.ToInt32(numWritesInput);

    // Add 9 new sheets, named Sheet2 - Sheet10
    for (int s = 0; s < 9; s++)
    {
    @@ -36,14 +44,15 @@ public static void DumpValuesEverywhere()

    // Scatter some values around the various sheets
    var sw = Stopwatch.StartNew();
    for (int i = 0; i < 20000; i++)
    for (int i = 0; i < numWrites; i++)
    {
    var r = new ExcelReference(i/10, i/10, 0, 1, $"Sheet{i%10 + 1}");
    r.SetValue(new object[,] { { $"Info - {i}", DateTime.Now } });
    }
    new ExcelReference(0, 4).SetValue("ElapsedMs:");
    new ExcelReference(0, 0, 5, 5, "Sheet1").SetValue(sw.ElapsedMilliseconds);
    sw.Stop();
    Excel(xlcWorkbookActivate, "Sheet1");
    new ExcelReference(0, 4).SetValue("Writes / ElapsedMs / Writes per ms:");
    new ExcelReference(0, 0, 5, 7).SetValue(new object[,] { { numWrites, sw.ElapsedMilliseconds, (double)numWrites / sw.ElapsedMilliseconds } });
    }
    }
    }
  2. govert created this gist Jul 2, 2020.
    49 changes: 49 additions & 0 deletions SetValuePerf.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ExcelDna.Integration;
    using static ExcelDna.Integration.XlCall;

    namespace SetValuePerf
    {
    public static class Macros
    {
    [ExcelCommand(ShortCut = "^D")] // Ctrl + Shift + D
    public static void DumpValues()
    {
    var sw = Stopwatch.StartNew();
    for (int i = 0; i < 20000; i++)
    {
    var r = new ExcelReference(i, i, 0, 1);
    r.SetValue(new object[,] { { $"Info - {i}", DateTime.Now } });
    }
    new ExcelReference(0, 4).SetValue("ElapsedMs:");
    new ExcelReference(0, 5).SetValue(sw.ElapsedMilliseconds);
    }


    [ExcelCommand(ShortCut = "^E")] // Ctrl + Shift + E
    public static void DumpValuesEverywhere()
    {
    // Add 9 new sheets, named Sheet2 - Sheet10
    for (int s = 0; s < 9; s++)
    {
    Excel(xlcWorkbookInsert, 1);
    }

    // Scatter some values around the various sheets
    var sw = Stopwatch.StartNew();
    for (int i = 0; i < 20000; i++)
    {
    var r = new ExcelReference(i/10, i/10, 0, 1, $"Sheet{i%10 + 1}");
    r.SetValue(new object[,] { { $"Info - {i}", DateTime.Now } });
    }
    new ExcelReference(0, 4).SetValue("ElapsedMs:");
    new ExcelReference(0, 0, 5, 5, "Sheet1").SetValue(sw.ElapsedMilliseconds);
    Excel(xlcWorkbookActivate, "Sheet1");
    }
    }
    }