none
Thread Performance in .NET application RRS feed

  • General discussion

  • Hi,

    I am measuring performance of .NET multithreaded programs. I face a strange problem.


    I have a code which executes a task and then sleeps. This works within a loop. The task is to find the prime numbers between 2 and 1000. It carries on this loop for 1000 iterations.

    When a single thread executes the code, it finishes in 277 secs. But when 2 threads run and perform the same work independantly, each finishes in 272 secs. When 4 threads are run, each finishes in 270 secs. When 6 threads run, the time taken is 250 secs. I have run this experiment many number of times, the result is always the same.

    The entire experiment uses a single processor machine.

    My question is, how come each thread does the job faster when there are parallel jobs running ? It should remain the same. Is there something else happenning ? Like, some form of cache effect ? Or some other effect ?

    If there is a cache effect, could it be understood from the cache object in the performance monitor tool ? If so which counter should I observe ?

    It would be great to get some thoughts on this.

    Thanking, in anticipation.

    Nilabja
    Wednesday, February 27, 2008 6:45 PM

All replies

  • Well your application isnt the only thing the cpu is running so your threads are being shared by every other application out there now.  I cant give you any thoughts on what you are seeing, but you now have 2 threads the processor is executing so that might explain why the threads arent taking twice as long.    Its possible that because of this the cpu starts giving that application some preference since it has more threads to execute (I am only thinking out loud here though).

     

    Wednesday, February 27, 2008 9:51 PM
  • Hi Dan,

    Thanks for your thoughts.

    I will again phrase my problem,

    Every thread has the same workload.

    When multiple threads run, they work on a separate copy of the workload and thus are independant. Intuitively, each should at least take 277 secs to finish the work, if not more.

    But,
    When 2 thrds run, each finishes in 273 secs.

    When 4 thrds run, each finishes in 270.
    When 6 thrds run each finish in 250 secs.

    Are you saying that when threads increase in the application, they are scheduled more often and that causes the threads to finish faster ? Does Windows do this kind of scheduling ? Is there anyway to verify this ? Will any performance counter help me in understnading this ?

    Thanks in anticipation,
    Nilabja



    Thursday, February 28, 2008 12:02 AM
  • If I understand you correctly you split the workload accross your threads into equal sized packets.
    You do sth like
      ThreadFunc()
      {
         for(....)
         {
             HardWork();
             Sleep(xxx);
        }
     }


    Could it be that your sleep is also split accross several threads?
    If you wait 23s in one thread then and 4s in 6 parallel threads you have parallel waiting?

    Yours,
      Alois Kraus

    Thursday, February 28, 2008 1:16 AM
  • Hi Alois,

    No. I have not split the workload across the threads. I am running the same workload on all the threads. They are running in parallel.

    So each thread runs a function,

    ThreadFunc()
      {
         for(....)
         {
             HardWork();
             Sleep(xxx);
        }
     }

    Here HardWork is the same for all the threads and the xxx for the sleep is also the same for all.

    When a single thread runs and executed ThreadFunc is takes 277 secs to complete.

    When 2 threads parallely run ThreadFunc, each take 273 secs. ie each takes 4 secs less to do the same work.

    When 4 threads parallely run, each takes 270 secs ie. 7 secs less than what it takes for 1 thread to complete

    When 6 threads run, each takes 250 secs. ie 27 secs less.

    Remember, that when 1 thread runs there is one Hardwork done, 2 threads do 2*Hardwork, 4 thread do 4*Hardwork etc, since each thread is doing the same work as when 1 thread is running.

    But the result I get tells me that when more threads do the same job parallely, they do it at a faster rate. My question is , why is it so ?

    Thanks
    Nilabja

    Thursday, February 28, 2008 8:40 PM
  • To analyze this issue, we really need the source code and the detailed repro steps to reproduce the problem, so that we can investigate the issue in house. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.


    Friday, February 29, 2008 9:11 AM
  • We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by editing your initial post and changing the radio button at the top of the post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions. 

    Thank you!
    Tuesday, March 4, 2008 2:23 AM

  • Hi Feng,

       I am sorry, I could not follow up on the issue, since I got caught up with some other things.

       It is perfectly fine for me to give you the source code and detailed steps to run it. Please let me know, how I can give you the entire code. It is actually a small program containing two classes.

       Do I also need to change the issue type to "Question" ? Please let me know.

       Thanking in advance.

        Nilabja
    Wednesday, March 5, 2008 3:14 PM