How to improve Parallel.For loading speed?
-
I use Parallel.For to do some calculation, when first time i calculate, the speed is slow, but when i calculate again, the speed is better. I guess maybe Parallel.For cost time to load threading . I want to know how to pre-load Parallel.For before calculate, so that it can be fast at 1st time.
-
I use Parallel.For to do some calculation, when first time i calculate, the speed is slow, but when i calculate again, the speed is better. I guess maybe Parallel.For cost time to load threading . I want to know how to pre-load Parallel.For before calculate, so that it can be fast at 1st time.
It's impossible to answer your question because you haven't shown any of your code. In general, making assumptions is a bad idea when tuning the performance of your code. Use a proper profiling tool to see where the bottleneck is, and concentrate your efforts on that.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
It's impossible to answer your question because you haven't shown any of your code. In general, making assumptions is a bad idea when tuning the performance of your code. Use a proper profiling tool to see where the bottleneck is, and concentrate your efforts on that.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
oh, if my code is : ConcurrentBag list = new ConcurrentBag(); Parallel.For(0, 10000, item => { list.Add(item); }); when i run it ,first time cost time is more than run it again.
I am hoping that the code you have given is not the true code that you are trying to improve. But from this How to: Write a Simple Parallel.For loop[^]
Quote:
When parallelizing any code, including loops, one important goal is to utilize the processors as much as possible without over parallelizing to the point where the overhead for parallel processing negates any performance benefits. In this particular example, only the outer loop is parallelized because there is not very much work performed in the inner loop. The combination of a small amount of work and undesirable cache effects can result in performance degradation in nested parallel loops. Therefore, parallelizing the outer loop only is the best way to maximize the benefits of concurrency on most systems.
One of the first things I always try to do when using Parallel loops is to limit the amount of processors that it could use with the following MaxDegreeOfParallelism[^] option. e.g.
ConcurrentBag list = new ConcurrentBag();
ParallelOptions options= new ParallelOptions { MaxDegreeOfParallelism = 2 };
Parallel.For(0,10000, options, item =>
{
list.Add(item);
}Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON