4

I am having a task of scanning all the folders name start with "xyz" parallely. I meant if one folder getting scan same time other one should also getting scan. I don't want one by one scanning.

For that I used Parallel Foreach.

Question is? Is it correct or not? and How to know is it running parallely(to put any message some where)?

Parallel.ForEach(path, currentPath =>
   {
    var output = programReader.GetData(currentPath, durReader.dirPattern);

    foreach (var item in output)
      {
        foreach (var project in item.Name)
           Console.WriteLine(item.serverName + " " + item.serverNumber + " " + fileName);
        }
     }

EDIT:

Is Parallel.Foreach only works on multicore systems or it could work on single core system also to perform show parallelism

1
  • 2
    As this is I/O bound, running it in parallel may actually be slower. Make sure you time the results. Commented Feb 14, 2011 at 8:38

4 Answers 4

3

Foirst - if you ask a question, make it a question. !! is a good indication it is not a question.

Second, your approach makes little sense. Parallel will go VERY parallel. Good. Bad: you still have only one disc. Result: tasks waiting. It makes no sense to paralellize IO operations over the degree supported by the hardware.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh!! thanx for pointing. I made changes. I am in doubt here is Parallel will divide the task on different proccessors. How could I know for each path folders are scanning parallely . I m totally confused.
Again, number of processors is irrelevant. Your disc is not fast enough to handle multiple requests at the same time. Even ONE processsor, even 10 years old, would be too fast. Disc IO can not be efficiently paralellized unless you have a large SAN or SSD's available.
Oh... so you want to say, this Parallel will not work in ONE core Systems. Then How should I do Parallel scanning of Directories?
No, imean it is pretty irrelevant how much performance you try to get out of the processor. Your limiting factor is not the processor in this particular example, it is the DISC. What you do here is building a formula one car, jsut to find out you need to transport a 10 ton machine. Does not fit. Directory info analysis is limited by DISC speed primarily.
1

The Parallel extensions split the load per core - although I'm not sure if they take into account hyperthreaded cores.

But, to add another answer to steer you in the right direction:

Don't try and do this in parallel. The disk can only serve one request at a time, so you're likely to just slow everything down as the disk queue is just likely to get bigger.

The only scenario where you might be able to get increased performance is if the location you're scanning is actually a SAN where the storage is distributed and highly replicated.

Comments

0

You can print the Thread.ManagedThreadId value to see which threads are being used.

1 Comment

Oh yes!! that is one way. Let me try it.
0

Parallel.ForEach or Parallel.Execute uses the cores of the processor. So if your processor is having more thn one core they will be used equally to run this thread. Here you are m

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.