views:

68

answers:

2

I have recently moved to C#.net 4.

I love Parallel.For, but not sure when to use and when not to. I know that when order is not important for me - I'll use it.

But are there any tests regarding the overhead of working with Parallels? Meaning, if my loop runs only 10 times (and performs very little logic) - should I avoid Parallels? Are there any thumb rules?

+4  A: 

I would avoid using Parallel.For unless performance is an issue.

Writing code that runs concurrently is in general harder than writing single threaded code. Furthermore if you make an error due to a concurrency issue it can be difficult to debug it. For example the bug might occur only sometimes and not be easily reproducible. Unless you have a specific need for increased performance I would suggest that you keep it simple and use an ordinary loop on a single thread.

Mark Byers
I agree - don't just use it because it's there, instead, try to solve an actual problem with it. Also, profiling is your friend ;)
Jim Brissom
I think that "performance is an issue" was implied here.
Henk Holterman
I agree with Mark. Use least possible resources for a job.
Josh
A: 

Quoting SQLite FAQ: 'Threads are evil. Avoid them'

Parallelization is useful for performance. Application performance optimization is one of the most counter-intuitive things in software design, and should be done with extreme care, using the right measurement tools, or it will just look funny.

Some would optimize UI code to respond in a microsecond instead of milliseconds, clearly having no value and causing lots of damage.

Pavel Radzivilovsky
Some truth in this, but altogether too simple. See for example http://stackoverflow.com/questions/3415519/is-there-a-point-to-multithreading/3415563#3415563
Henk Holterman