Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to improve Parallel.For loading speed?

How to improve Parallel.For loading speed?

Scheduled Pinned Locked Moved C#
performancetutorialquestioncode-review
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    smallkubi
    wrote on last edited by
    #1

    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.

    Richard DeemingR 1 Reply Last reply
    0
    • S smallkubi

      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.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      S 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        S Offline
        S Offline
        smallkubi
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • S smallkubi

          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.

          S Offline
          S Offline
          Simon_Whale
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups