Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can each Iteration of a for loop/for_each be done in parallel? (C++11)

I'm iterating over a vector of structs and processing each struct individually.
It looks something like this:

for_each(begin(data),end(data),DoTask);
//assume "data" is std::vector<DataT>  
//assume DoTask is a function that takes a DataT by reference

The code is significantly slow because DoTask connects to particular websites and analyzes HTML.
What would be the best way to speed this up?
My goal is to analyze multiple DataTs at the same time.
I'm very new to threading, but std::async and std::future look promising.

like image 879
Trevor Hickey Avatar asked Apr 14 '12 16:04

Trevor Hickey


People also ask

Is std :: for_each faster?

Looping Performance in C++ Today I was testing the performance of a piece of code, which is basically accessing each element in a container within a for loop. But the result is quite shocking because I found the std::for_each version is 10 times faster than the raw loop.

What is used to specify the sequence used by the for_each expression?

Syntax: for_each (InputIterator start_iter, InputIterator last_iter, Function fnc) start_iter : The beginning position from where function operations has to be executed. last_iter : The ending position till where function has to be executed.

What is std :: for_each?

std::for_each is an STL algorithm that takes a collection of elements (in the form of a begin and end iterator) and a function (or function object), and applies the function on each element of the collection. It has been there since C++98.


2 Answers

You can do something like this

for(T& d : data) std::thread(DoTask, d).detach();

Or you can use something more complicated like Intel's Thread Building Blocks and the parallel_for (isn't that the name?) function thereof.

like image 114
Seth Carnegie Avatar answered Oct 02 '22 18:10

Seth Carnegie


Are you using GCC? Recent versions have a parallel version of for_each (see here for how to use it).

like image 30
Jeremiah Willcock Avatar answered Oct 02 '22 18:10

Jeremiah Willcock