Friday, 15 March 2013

c# - Linq Expression Slow - Optimization needed if possible -



c# - Linq Expression Slow - Optimization needed if possible -

i have list of integers "numberrangelist" containing 31 integers 62 92 in sequential order, extracting sec list of integers "extractedlist" contain 10 integers sum equal "total". problem take 30 seconds compute, there way of speeding up??

var numberrangelist = new list<int>() { 62, 63, ...92 }; var total = 772; var extractedlist= (from n1 in numberrangelist n2 in numberrangelist n3 in numberrangelist n4 in numberrangelist n5 in numberrangelist n6 in numberrangelist n7 in numberrangelist n8 in numberrangelist n9 in numberrangelist n10 in numberrangelist n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10 == total select new list<int> { n1, n2, n3, n4, n5, n6, n7, n8, n9, n10 }).take(1).first();

the performance issue not in linq, in problem itself. problem known in cs literature , it's called subset sum http://en.wikipedia.org/wiki/subset_sum_problem . it's known problem belongs np-complete class of time-complexity ( http://en.wikipedia.org/wiki/np-complete ). should research sub-optimal solution if don't want explore (exponential) problem complexity.

c# performance linq time-complexity

No comments:

Post a Comment