C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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.
And: By addressing this problem, performance increases. Everyone is happier by a few nanoseconds.