在委托上使用扩展方法的 c# 中的函数链接。是否有任何性能影响。我正在发布带有用法的代码片段
Function chaining in c# using extension methods on delegates. Is there any performance implications. I am posting code snippet with usage
委托延期。欢迎提出建议和任何改进。此代码当前正在其中一个项目中使用。
internal static class xt
{
public static Func<From, To> Next<From, To>(this From @obj, Func<From, To> transform)
{
return (fr) => transform(fr);
}
public static Func<Tform, TForward> Next<Tform, Tto, TForward>(this Func<Tform, Tto> g, Func<Tto, TForward> f)
{
return (tf) => f(g(tf));
}
public static Tto Input<Tfrom, Tto>(this Func<Tfrom, Tto> fx, Tfrom data)
{
return fx(data);
}
public static void Execute(this Action fx)
{
fx();
}
public static Func<Tform, Tto> Next<Tform, Tto>(this Func<Tform> g, Func<Tform, Tto> f)
{
return tf => f(g());
}
}
您面临的唯一性能影响是委托的内存分配。然而,这些都是小而短暂的,将存在于 Gen0 中并在快速操作中被收集。
我认为您不必担心。您应该关注的代码中通常有更多威胁性能的元素 - 字符串处理、数组处理等。
委托延期。欢迎提出建议和任何改进。此代码当前正在其中一个项目中使用。
internal static class xt
{
public static Func<From, To> Next<From, To>(this From @obj, Func<From, To> transform)
{
return (fr) => transform(fr);
}
public static Func<Tform, TForward> Next<Tform, Tto, TForward>(this Func<Tform, Tto> g, Func<Tto, TForward> f)
{
return (tf) => f(g(tf));
}
public static Tto Input<Tfrom, Tto>(this Func<Tfrom, Tto> fx, Tfrom data)
{
return fx(data);
}
public static void Execute(this Action fx)
{
fx();
}
public static Func<Tform, Tto> Next<Tform, Tto>(this Func<Tform> g, Func<Tform, Tto> f)
{
return tf => f(g());
}
}
您面临的唯一性能影响是委托的内存分配。然而,这些都是小而短暂的,将存在于 Gen0 中并在快速操作中被收集。
我认为您不必担心。您应该关注的代码中通常有更多威胁性能的元素 - 字符串处理、数组处理等。