C# Anonymous functions for conditional operations
More specifically, lambda expressions… Let’s say you have a method, the body of which will execute based on the value of a flag (myFlag in the example here):
public void MyConditionalMethod(string format, params object[] args)
{
if (myFlag)
{
DoSomething(string.Format(format, args));
}
}
Ideally, in scenarios where myFlag is false we’d like a call to this method, such as the following, to have limited overhead:
MyConditionalMethod("{0}, {1}", param0, param1);
If param0 and/or param1 are value-types, this call will incur the overhead of boxing regardless of the value of myFlag. Obviously, things get worse as we pass more value-types as parameters.
One approach to addressing this problem is through the use of lambda expressions; the lambda expression is only executed if the flag is set. Here’s a version of MyConditionalMethod modified accordingly:
public void MyConditionalMethod(Func<string> getString)
{
if (myFlag)
{
DoSomething(getString());
}
}
A call to the modified method looks like this:
MyConditionalMethod(() => string.Format("{0}, {1}", param0, param1));
Here are some rough performance numbers (with compiler optimizations on) to illustrate the potential advantages of this approach:
Min (ns) | Max (ns) | Median (ns) | |
Standard (1000 iterations, 10 samples) | 74.396 | 434.688 | 147.888 |
with lambda (1000 iterations, 10 samples) | 5.692 | 11.687 | 5.692 |
This technique can be used in tracing or other scenarios where these methods may be called frequently throughout your code and it can have a significant performance benefit in the aggregate.
Permalink Comments off