XE7 에 parallel programming 이 된다고 해서 iso 다운받아 vm에 설치해서 테스트 해 보았습니다.
금방 지워버릴거라.. 정성은 드리지 않았습니다.
그냥 2 부터 50000 까지 prime number 를 찾는 루프를 만들어서 테스트 했습니다.
C++ For Loop : 438 msec
TParallel Loop : 214 msec
확실히 빠르긴 한데.. 소스코드를 쭉 살펴보면 결국 thread를 쓴다는 것 입니다.
굳이 갈필요가.. OMP 지원하는것도 아니고..(OMP 지원하나요?)
그냥 boost thread로 조금 더 짜주면 될듯 합니다.
실행중 memo 에 update하는것은 생략.. 이거 넣으면 결국 thread sync 걸려서 속도 더 늦습니다.
순수한 계산만 하는데에 사용해야..
차리리 직접 thread 만들어 나름대로 컨트롤 하는것이 좋을지도 모릅니다.
다운받고, 설치하고.. 몇시간 잡아먹었네요...
다소 실망....
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TMemo *Memo1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
class TMyThread : public TThread
{
TForm1 *form;
public:
__fastcall TMyThread(TForm1 *form)
: TThread(true)
{
this->form = form;
}
void __fastcall MyIteratorEvent(TObject* Sender, int AIndex)
{
if(IsPrime(AIndex))
{
//form->Memo1->Lines->Add(AIndex);
}
}
void __fastcall Execute(void)
{
TParallel::For(NULL, 1, 50000, TMyThread::MyIteratorEvent);
}
};
TMyThread *thread;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
bool IsPrime(int N)
{
for(int i=2;i<N;i++)
{
if(N % i==0)
{
return false;
}
}
return true;
}
#include <System.Threading.hpp>
#include <System.Diagnostics.hpp>
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Memo1->Lines->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TStopwatch Timer;
Timer = TStopwatch::StartNew();
#pragma omp parallel
for(int i=1;i<50000;i++)
{
if(IsPrime(i))
{
// Memo1->Lines->Add(i);
}
}
UnicodeString s;
s.printf(L"C++ For Loop : %d msec",Timer.ElapsedMilliseconds);
Memo1->Lines->Add(s);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Memo1->Lines->Clear();
thread = new TMyThread(this);
TStopwatch Timer;
Timer = TStopwatch::StartNew();
thread->Start();
thread->WaitFor();
UnicodeString s;
s.printf(L"TParallel Loop : %d msec",Timer.ElapsedMilliseconds);
Memo1->Lines->Add(s);
}
//---------------------------------------------------------------------------
|