C# Parameter Optimization

Learn how parameter count influences method call performance.

Parameter optimization. Excess parameters reduce method call performance. It is possible to optimize the parameters that a method receives. We remove unneeded parameters. This will reduce the memory usage on the stack and improve performance.Parameter PerformanceOptimization

Benchmark. To begin, the optimization we are using here actually will typically lead to clearer source code, and also faster execution times. Many programs over time will end up with methods that have unused parameters.
Remove: If you remove these parameters, you will improve performance, as this example benchmark demonstrates.
Methods: The Method1 function has three unnecessary parameters. The Method2 function has no extra parameters.
C# program that tests parameters, performance using System; using System.Diagnostics; class Program { static int Method1(int a, int b, int c, int d, int e, int f) { // This method contains extra parameters that are not used. if (a == 0) { throw new Exception(); } return a * 100 + b * 10 + c; } static int Method2(int a, int b, int c) { // This method only contains necessary parameters. if (a == 0) { throw new Exception(); } return a * 100 + b * 10 + c; } const int _max = 100000000; static void Main() { int result = 0; var s1 = Stopwatch.StartNew(); for (int i = 1; i < _max; i++) { result = Method1(i, i, i, i, i, i); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 1; i < _max; i++) { result = Method2(i, i, i); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) / _max).ToString("0.00 ns")); Console.Read(); } } Output 2.97 ns Method with 6 parameters 2.56 ns Method with 3 parameters

This program results in a consistent win for the method without the unnecessary parameters (Method2 is faster than Method1). This is likely because Method1 must copy the extra three four-byte arguments each time to the method invocation.
Note: The saves is almost half a nanosecond per method call. The savings is small, but could add up in some cases.
Note 2: Exception handling was added to Method1 and Method2 to ensure that the JIT compiler applies no inlining to the methods.

FxCop. You will find that the FxCop tool released by the smart people at Microsoft can indicate when you have a method that does not use all of its parameters. More information on FxCop and performance optimizations is available.

Summary. Some refactorings—such as removing unnecessary parameters—improve runtime performance and also code simplicity. In your projects, cruft may accumulate because of external changes. Sometimes parameter lists are not cleaned up properly.
And: By addressing this problem, performance increases. Everyone is happier by a few nanoseconds.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.