I have 3 For loops running for a lot of iterations:
for (int i = 0; i < 1000000; i++)
{
for (int j = 0; j < 1000000; j++)
{
var myObj = new MyObj(i,j);
var a = myObj.doSomething();
for (int k = 0; k < 1000000; k++)
{
a.work();
}
}
}
I have to run a total of 10^18 iterations which will take a lot of time to complete.
Is there a fast way to make the loops run faster using Parallel.For
? If so - how to change my code?
If the work to be done is too fine-grained, the overhead associated with parallelism might be bigger than the gain from parallel execution. Therefore, using Parallel.For
only for the outer loop is sufficient. It has the advantage to create greater chunks of work and to minimize the overhead.
That still adds up to 1 million tasks that need to be performed in parallel. That's pretty much enough.
var options = new ParallelOptions {
MaxDegreeOfParallelism = Environment.ProcessorCount
};
var result = Parallel.For(0, 1000000, options, (i, state) =>
{
for (int j = 0; j < 1000000; j++)
{
var myObj = new MyObj(i,j);
var a = myObj.doSomething();
for (int k = 0; k < 1000000; k++)
{
a.work();
}
}
});
But note that if a.work()
uses resources, that require exclusive access, then the locking involved might ruin the advantage.
It is advisable to limit the degree of parallelism to the processor count if the work is mainly CPU bound.