c# - Parallelizing very large array base conversion -
i have method converts value newbase number of length length.
the logic in english is:
if calculated every possible combination of numbers 0 (c-1) length of x set occur @ point
while method below work perfectly, because large numbers used, can take long time complete:
for example, value=(((65536^480000)-1)/2), newbase=(65536), length=(480000) takes hour complete on 64 bit architecture, quad core pc).
private int[] getvalues(biginteger value, int newbase, int length) { stack<int> result = new stack<int>(); while (value > 0) { result.push((int)(value % newbase)); if (value < newbase) value = 0; else value = value / newbase; } (var = result.count; < length; i++) { result.push(0); } return result.toarray(); }
my question is, how can change method allow multiple threads work out part of number?
i working c#, if you're not familiar pseudocode fine too.
note: method question: cartesian product subset returning set of 0
if getvalues
method bottleneck, there several things can speed up.
first, you're dividing newbase
every time through loop. since newbase
int
, , biginteger
divide method divides biginteger
, you're potentially incurring cost of int
-to-biginteger
conversion on every iteration. might consider:
biginteger bignewbase = newbase;
also, can cut number of divides in half calling divrem:
while (value > 0) { biginteger rem; value = biginteger.divrem(value, bignewbase, out rem); result.push((int)rem); }
one other optimization, mentioned in comments, store digits in pre-allocated array. you'll have call array.reverse them in proper order, takes approximately no time.
that method, way, doesn't lend parallelizing because computing each digit depends on computation of previous digit.
Comments
Post a Comment