
Parameter order influences method performance. It influences the enregistration of variables. We can sometimes enhance performance by reordering parameters in methods. We review the reasons why this is effective.
Performance of methods and parameter ordering
X86 platform.
Test first 2 parameters: 952 ms [faster]
Test last 2 parameters: 2224 ms
First, the .NET Framework compiles methods to native code, and it uses the same optimizations available in unmanaged languages with FASTCALL. My investigation shows that by placing frequently-used variables in the first two parameters, you can improve performance. The benchmark tests two methods in tight loops. The methods are exactly the same except for the parameter order.
Program that benchmarks parameter usage [C#]
using System;
using System.Diagnostics;
class Program
{
static bool Test1(int a, int b, int c, int d, int e, int f)
{
//
// This method tests the first two parameters in the loop.
//
for (int i = 5; i < 10000000; i++)
{
if (a == i)
{
return true;
}
if (b == i)
{
return true;
}
}
if (c == 1 ||
d == 1 ||
e == 1)
{
return true;
}
return false;
}
static bool Test2(int a, int b, int c, int d, int e, int f)
{
//
// This method tests the last two parameters in the loop.
//
for (int i = 5; i < 10000000; i++)
{
if (e == i)
{
return true;
}
if (f == i)
{
return true;
}
}
if (a == 1 ||
b == 1 ||
c == 1)
{
return true;
}
return false;
}
static void Main()
{
const int m = 100;
Stopwatch s1 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
Test1(3, 3, 3, 3, 3, 3);
}
s1.Stop();
Stopwatch s2 = Stopwatch.StartNew();
for (int i = 0; i < m; i++)
{
Test2(3, 3, 3, 3, 3, 3);
}
s2.Stop();
Console.WriteLine("{0},{1}", s1.ElapsedMilliseconds, s2.ElapsedMilliseconds);
Console.Read();
}
}Further description of benchmark. The exact logic tests in Test1 and Test2 are unimportant here. You should note that two parameters are tested in the tight loop in Test1 and Test2. In the first method Test1, the variables tested are the very first parameters, so they are more likely to be enregistered.

Here we note that when you compile a method in the C# language, the parameters are pushed onto the stack and the method is called. Internally, that method then uses the parameters on the stack. However, Microsoft compilers have an advanced optimization called FASTCALL, where the first two parameters in x86 are passed as registers.
Understanding registers. An enregistered variable in machine code is one that is stored in the fastest processor cache so access to it is extremely fast. In loops, the iterator variable i is normally stored in a register.
In most loops and programs, the order of the parameters is not likely to affect the result. However, occasionally you may find a method that uses many parameters. Often, such methods are focused on optimization, because they defy object-oriented best practices. This technique could improve these methods measurably.
Understanding FASTCALL. Here we look at how the FASTCALL convention is implemented in x86 by Visual C++. "For FASTCALL, compiler will try to pass arguments in registers, if not enough caller will pushed them into stack still in an order from right to left."
x86 Disassembly/Calling Convention Examples"Stack cleanup is done by callee. It is called FASTCALL because if arguments can be passed in registers (for 64bit CPU the maximum number is 6), no stack push/clean up is needed."

We saw how parameter order in C# methods affects the performance of the variables and the likelihood they will be enregistered. Having your loop variables in registers is hugely faster, and C even has the (ignored) register keyword. Finally, adequate knowledge of compilers and registers can improve your C# code.
Method Tips