Ebeworld’s Weblog

Trying to create

parallel_for_each Example

#include<iostream>
#include <conio.h>
#include <ppl.h>
#include <windows.h>
#include <vector>
#include <algorithm>
using namespace std;
using namespace Concurrency;

__int64 GetTime(){
	return GetTickCount();
}

void makeDouble(vector<int> vec){
	for_each(vec.begin(),vec.end(),[&](int i){
		cout<<2*i<<" ";
	});
	cout<<endl<<endl<<endl<<endl;
}

void parallel_makeDouble(vector<int> vec){
	parallel_for_each(vec.begin(),vec.end(),[=](int i){
		cout<<2*i<<" ";
	});
	cout<<endl<<endl<<endl<<endl;
}

int main(int argc, char** argv){
	vector<int> v(200);
	int n=0;
	generate(v.begin(),v.end(),[&](){ return n++;});

	__int64 begin=GetTime();
	makeDouble(v);
	__int64 end=GetTime();
	cout<<"Seq:"<<end-begin<<endl;

	begin=GetTime();
	parallel_makeDouble(v);
	end=GetTime();
	cout<<"Parallel:"<<end-begin<<endl;
	getch();
	return 0;
}

March 23, 2011 - Posted by | Algorithms, CPP, Uncategorized

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.