J
jehugaleahsa
I wrote a simple method that allows me to pass a delegate to find a
value in a list. I already had a set of methods for doing comparisons:
less, greater, equal, equivalent, etc. However, find algorithms always
compare the element in the list to a given value. In other words, I
needed to bind one value for all the comparisons.
I wrote a simple Bind1st method that looks like this:
public delegate R Operation<R, T>(T value);
public delegate R Operation2<R, T>(T lhs, T rhs);
public static Operation<R, T> Bind1st<R, T>(Operation2<R, T>
operation, T lhs)
{
return delegate(T rhs)
{
return operation(lhs, rhs);
};
}
However, when I run my find method with the Bind1st it runs *much*
slower. I understand that the bind has to call a method each time;
however, I can't see why that would cause the performance hit I am
experiencing. I have tried putting the delegate inline with the value
hard-coded as well and it runs much better. My code performs best when
no delegates are involved at all (but this means not using my find
method).
Any suggestions for getting away from the wall?
Thanks,
Travis
value in a list. I already had a set of methods for doing comparisons:
less, greater, equal, equivalent, etc. However, find algorithms always
compare the element in the list to a given value. In other words, I
needed to bind one value for all the comparisons.
I wrote a simple Bind1st method that looks like this:
public delegate R Operation<R, T>(T value);
public delegate R Operation2<R, T>(T lhs, T rhs);
public static Operation<R, T> Bind1st<R, T>(Operation2<R, T>
operation, T lhs)
{
return delegate(T rhs)
{
return operation(lhs, rhs);
};
}
However, when I run my find method with the Bind1st it runs *much*
slower. I understand that the bind has to call a method each time;
however, I can't see why that would cause the performance hit I am
experiencing. I have tried putting the delegate inline with the value
hard-coded as well and it runs much better. My code performs best when
no delegates are involved at all (but this means not using my find
method).
Any suggestions for getting away from the wall?
Thanks,
Travis