Trouble Benchmarking Addin

I’ve been working on a SolidWorks Addin in C# to help with reverse engineering and it’s critical that this runs as fast as possible.

I’m using DotNetBenchmarker to help with this but it’s saying that my Addin is running way slower than it actually is. Using a Stopwatch with Debug.Print I was reading speeds of about 300 ms and DotNetBenchmarker was taking so long that it couldn’t finish.

To confirm this I wrote a simple program that just looped through the features using Feature.GetNextFeature() and it iterated over 226 features in 2 ms while DotnetBenchMarker said it took 5 seconds.

For reference: My addin is already registered in Solidworks using Xcad and I am using a console application benchmarker that links to solidworks using Marshal.GetActiveObject, and contains my addin as a project reference so that I can reuse all my code. I’ve tried putting the benchmarker inside of the addin but that yields the same results.

I’m open to any and all solutions because my next best option is just my own benchmarker that uses the stopwatch.

I don’t know what causes the strange results, can’t help you with that. Can you even combine Benchmarkdotnet and solidworks?

Some general tips for speed and solidworks:

  • Don’t run benchmarks in Debug mode
  • Generally, solidworks property getters and traversal methods are very fast and methods that change geometry are very slow. I optimized my Drew AutoFit code but the slowest thing by far was changing the sheet scale once. Nothing else really mattered.
  • Running inside solidworks as an add-in is generally 100x faster than running out-of-process. My friend says there’s a way around that but we haven’t tried.
  • Solidworks has many features you can disable to gain speed. But every disabled feature can break your workflow.
  • On the scale of slow solidworks APIs, the stopwatch is probably fine.

I once wrote a blog post that’s useful for you:

How to improve SOLIDWORKS macro speed 10x [+ bonus] - CAD Booster

I wasn’t able to get benchmarkdotnet to work, but a basic stopwatch function got the results I needed.

For anyone who comes across this later:

        private double BenchMarkFunction(Action a, int reps = 10)
        {
            TimeSpan[] times = new TimeSpan[reps];
            System.Diagnostics.Stopwatch sw = new();
            for (int i = 0; i < reps; i++)
            {
                sw.Start();
                a.Invoke();
                sw.Stop();
                times[i] = sw.Elapsed;
                sw.Reset();
            }
            double average = 0;

            foreach (var t in times)
            {
                average += t.TotalMilliseconds;
            }
            average /= reps;

            return average;
        }