Example #1
0
        public static BenchmarkComparison[] StartAndCompare(Action baseline, BenchmarkParameters parameters, params Action[] actions)
        {
            var mainResult   = Start(baseline, parameters);
            var otherResults = StartMultiple(actions, parameters);

            return(Compare(mainResult, otherResults));
        }
Example #2
0
        public static BenchmarkResult Start(Action actionToProfile, BenchmarkParameters parameters)
        {
            PerformWarmup(actionToProfile, parameters);

            var result = new BenchmarkResult(actionToProfile.Method.Name);

            var iterations = parameters.BenchmarkIterations;

            using (var benchmark = StartScope(result))
            {
                for (var i = 0; i < iterations; i++)
                {
                    /* TODO:
                     * I had to call Invoke on the passed action. This is a virtual call,
                     * that is slower. Find a way to get rid of it.
                     */
                    actionToProfile.Invoke();
                }
            }

            result.AverageMilliseconds /= iterations;
            result.AverageTicks        /= iterations;

            return(result);
        }
Example #3
0
        public static BenchmarkResult[] StartMultiple(Action[] actionsToProfile, BenchmarkParameters parameters)
        {
            var results = new BenchmarkResult[actionsToProfile.Length];

            for (var i = 0; i < actionsToProfile.Length; i++)
            {
                results[i] = Start(actionsToProfile[i], parameters);
            }

            return(results);
        }
Example #4
0
        private static void PerformWarmup(Action actionToProfile, BenchmarkParameters parameters)
        {
            // Do a warm-up spin if warmup is enabled
            if (!parameters.PerformWarmup)
            {
                return;
            }

            parameters.InitializeAction?.Invoke();

            for (long i = 0; i < parameters.WarmupIterations; i++)
            {
                actionToProfile.Invoke();
            }
        }