Reputation: 395
I have, let's say, a list with 500 objects. For each object, I'm calling a function calculating it's cost. So each of the 500 calls is independent from the others. The overall takes around 30 seconds. Wouldn't it be possible to run all the 500 tasks at the same time as they don't rely on each other ? I know nothing about multi-threading therefore I don't know if it could be a solution.
Upvotes: 2
Views: 2007
Reputation: 562
Can you spin up 500 threads - yes. Will they run simultaenously - no. That being said, the optimum number of threads to use is unfortunately a more complex issue than a thread per core.
Intel processors, for example, have two execution pipelines per core (called u and v), allowing out-of-order processing which, depending on the conditions, can execute two instructions faster than sequentially processing those same two instructions. The problem is that the execution pipelines do share some resources within the core. They share:
Cache, Branch Prediction resources, Instruction fetching and decoding, and Execution units.
This means that the efficiency of executing two instructions is dependent on things like cache misses and branch prediction misses. The advantage comes in where an instruction is blocking while waiting for a high latency operation (e.g. fetching the contents of memory into the cache on a cache miss), if there is another instruction in the other pipeline, it can be worked on while waiting. This is absolutely not faster than two separate cores, but often works out to be faster than sequential instruction processing (on average roughly 25% faster).
Another thing to keep in mind is that the operating system also needs some time on the processor to execute. Microsoft's recommendation for maximum threads for efficient processing is 25 threads per logical core (There is 1 logical core per physical core without Hyper-Threading, 2 per physical core with HT) (this is the default max threads per core setting in IIS). It should be noted, however, that this is a "rule of thumb". The only way to find the true optimum is to test on a given software/hardware setup. Optimizing for hardware is, however, not practical in practice and not recommended, hence the need for a "rule of thumb".
Upvotes: 0
Reputation: 1988
Wouldn't it be possible to run all the 500 tasks at the same time as they don't rely on each other?
In short, yes if you have 500 cores (CPUs).
Switching the context between threads is a very expensive process and involves suspending the current thread, which is why more effective is to run one thread per CPU.
Since C# 4.0 you can use Task Parallel Library and Parallel LINQ (PLINQ), it simplifies Parallel Programming in the .NET Framework.
// IEnumerable<MyClass> items = ...
var results = items
// Enables parallel execution of the query
.AsParallel()
// Specifies the method for creating values
.Select(item => Calculate(item))
// Waits for calculating all the values and returns the result (as an array)
.ToArray();
Upvotes: 2
Reputation: 46465
Running a single threaded process will only use one-core of your machine (this does allow other cores to run operating system and other application processes).
Your process sounds liek a good contender for multi-threaded processing, however you don't need a new thread for every process - this will create overhead in creating the threads, and also you won't have enough cores to run them all individually, so they will be fighting for CPU resources.
Using Parallel.For
in .Net4.0 will cleverly use as many threads as it can.
Upvotes: 3
Reputation: 18074
Try Parallel.ForEach
You can see an example here: How to: Write a Simple Parallel.ForEach Loop
Upvotes: 1
Reputation: 217341
You can easily parallelize the work using the Parallel.ForEach Method:
Parallel.ForEach(items, item =>
{
item.CalculateCost();
});
Upvotes: 3
Reputation: 22945
Use the Task Parallel Library to start an individual task for each object. In the task, you would call the function to calculate it's cost.
Upvotes: 2