1

Topic: 2D-Linq and optimization of numeral filters - 3.5

Well, here and Friday. It's been a while, colleagues, it's been a while. Last time at us hot discussion in which valuable ideas have been formulated some was tore. Unfortunately, the declared heights the Author: Sinclair Date: 29.06 12:40 yet it was not possible, but "in our project reach important : we managed to collect it without compilation errors!" Therefore this part has number not 4, but 3.5. It was necessary to solve a question with "the dishonesty", lifted by some colleagues last time. So the code now - on : https://github.com/evilguest/linq2d Any interested person can collect and launch it at itself. And at the same time and to try the forces in a code beauty contest. That it to make, it is necessary to Add the class-filter in project FilterTests. It should implement either IArrayFilter <int>, or IWrapperFilter <int>. It is possible  from ArrayFilterBase then in property Data the array prepared for a filtration will be accessible. To add this type in the parameter list for FilterType, actually, all. Actually, it is enough to implement interface IFilter <int> and in method Initialize to prepare any data structure (if do not arrange neither the built in arrays, nor interface IArray). Methods C4 and 8 are searched by name. The main thing that the type of returned result implemented IStructuralEquatable and it was able to be compared element-wise with IArray2d <int> - see ArrayWrapper <T>, and, in particular, static private bool Equals (IArray2d <T> left, IArray2d <T> right, IEqualityComparer comparer) It is necessary that in a pursuit of beauty and productivity the correctness was not lost. Before performance  the single-valued start with comparing of results is led. Well, and further we will compare beauty and productivity In the current version of the test all filters which I implemented in the course of creative searches, but only the most typical are compared not: BasicArrayFilter - the most simple and obvious code, a double cycle by an input array. Any indirect calls, but output check for ranges five times for iteration. Represents itself as criterion of a correctness. UnsafeFilter - the code written   which loves . Neither indirect calls, nor checks of boundaries. There, basically, there is still a method to squeeze out pair of clock periods in an internal cycle, but already hardly BestLinkFilter is "my candidate" who claims for a victory: using System. Linq. Processing2d; using System. Linq. Processing2d. FastUnsafe; namespace FilterTests {public class BestLinqFilter: ArrayFilterBase, IArrayFilter <int> {public int [] C4 () => from d in Data. AsRelative (Bounds. Skip) select (d [-1, 0] + d [0,-1] + d [0, 1] + d [1, 0]) / 4; public int [] C8 () => from d in Data. AsRelative (Bounds. Skip) select (d [-1,-1] + d [-1, 0] + d [-1, 1] + d [0,-1] + d [0, 1] + d [1,-1] + d [1, 0] + d [1, 1]) / 8;} } That critic that the filter implicitly makes the decision  boundary in which it will not be possible to calculate a kernel, is accepted - now we explicitly set desirable semantics in.AsRelative (). No other variants, clear business, yet work, are left as a backlog on the future. Filters DeferredLinqFilter and FastLinqFilter in  do not enter. They illustrate the concept: At first, different implementations Linq are dissolved in library Linq2d on different . Thus, changing using, it is possible to switch easily between the "experimental" and "stable" versions in the code of the client Secondly, for lazy evaluations it is necessary to cause forcedly on result ToArray (), differently this  benefits with the big lift-off. If you build a similar construction do not forget to provide readiness of result inside  since  with check of each value into productivity measurements does not enter. Well, and for a dessert: Comparing, actually, productivity at three mentioned filters. BasicArrayFilter, certainly to take for a reference point it is not necessary - as it has been emotionally marked in the previous discussion, "such nobody writes". Therefore for 100 % we take UnsafeFilter. For an array 1000*1000, on my Lenovo T460 such result turns out here: Filter Class C4 C8 BestLinqFilter That is "the beautiful" filter fulfills 69.18 % of 70.44 % UnsafeFilter of 100.00 % of 100.00 % BasicArrayFilter of 293.76 % of 378.16 % approximately on third faster, than "ugly, but fast". And it yet a limit Is mandatory unsubscribe, if the downloaded project does not earn is my first experience with , easily could . It is very interesting to look at statistics on more other iron.