.net - Which is more efficient a method returning a Func<bool> or returning bool? -
i refactoring linq queries , trying determine efficient refactor.
the original line query similar to:
static void main() { var list = new list<string> { "a", "bb", "ccc" }; var shortlist = list.any(name => name.length == 1); }
i can refactor out string length check method follows:
static void main() { var list = new list<string> { "a", "bb", "ccc" }; var shortlist = list.any(name => isshort(name)); } private static bool isshort(string name) { return name.length == 1; }
or, can refactor out complete func method:
static void main() { var list = new list<string> { "a", "bb", "ccc" }; var shortlist = list.any(isshortfunc()); } private static func<string, bool> isshortfunc() { return name => name.length == 1; }
the question is, more efficient @ run time?
actually can better using method group conversion construct delegate:
static void main() { var list = new list<string> { "a", "bb", "ccc" }; var shortlist = list.any(isshort); } private static bool isshort(string name) { return name.length == 1; }
this have 1 less level of indirection first solution, , it's more readable (imo) second version. if don't have think in higher order functions, don't :)
i'd expect differences in efficiency absolutely miniscule though. should focusing on readability unless have evidence readable solution isn't performing need to.
Comments
Post a Comment