parallel processing - Clever aggregating of tuples in C# -
i processing complex query in parallel. called methods lot of tuple<ienumerable<object>, int> objects. aggregate them quickly, .aggregate (code below) not best option. right way it?
public static tuple<ienumerable<object>, int> parse(object obj) { var ieo = new list<object>(); var x = 5; return new tuple<ienumerable<object>, int>(ieo, x); } public static void query(list<object> obj) { var result = obj .asparallel() .select(o => parse(o)) . // aggregate , tuple of: // - flattened ienumerable<object> // - summed second items } and aggregate suggestion, slow , looks terribly. works.
.aggregate((t1, t2) => new tuple<ienumerable<object>, int>(t1.item1.concat(t2.item1), t1.item2 + t2.item2));
you can write custom flattener.
public static tuple<ienumerable<t>, int> magicflatten<t>( ienumerable<tuple<ienumerable<t>, int>> tuplecrap) { var item1 = tuplecrap.selectmany(x => x.item1); var item2 = tuplecrap.sum(x => x.item2); return new tuple<...>(item1, item2); } and later can use it:
.asparallel() .select(o => parse(o)) .magicflatten();
Comments
Post a Comment