Zdravim,
cely den si lamu hlavu nadtim, proc tento kod:
#include "stdafx.h"
#include <math.h>
#include <iostream>
#include <ctime>
#include <thread>
void fce(float* a, float* b, float* c, int from, int to){
for (int i = from; i < to; i++)
{
c[i] = (float)(log(a[i]) / sqrt(b[i]) + exp((a[i] + 1) / (b[i] - 1)));
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int p = 0;
int repeat = 3;
while (repeat-- > 0)
{
int n = 20 * 1000 * 1000;
auto a = new float[n];
auto b = new float[n];
auto c = new float[n];
auto d = new float[n];
for (int i = 0; i < n; i++)
{
a[i] = i;
b[i] = i;
}
clock_t begin_time = clock();
for (int i = 0; i < n; i++)
{
c[i] = (float)(log(a[i]) / sqrt(b[i]) + exp((a[i] + 1) / (b[i] - 1)));
}
std::cout << "\nres: " << c[2] << " ela:" << float((clock() - begin_time)) / (CLOCKS_PER_SEC / 1000.0);
begin_time = clock();
std::thread t5(fce, a, b, d, 0, n );
t5.join();
std::cout << "\nres T: " << c[2] << " ela:" << float((clock() - begin_time)) / (CLOCKS_PER_SEC / 1000.0);
delete[] a;
delete[] b;
delete[] c;
delete[] d;
}
std::cout << "\nALL DONE";
std::cin >> p;
return 0;
}
ma v release (se zapnutyma sse2, jinak default; visual studio 2013) ma na mem notasu (ivy bridge i7; w8.1) tento vystup:
res: 20.5757 ela:728
res T: 20.5757 ela:329
res: 20.5757 ela:708
res T: 20.5757 ela:329
res: 20.5757 ela:704
res T: 20.5757 ela:332
ALL DONE
abych to popsal tak nechapu jak je mozne za ta stejna funkce kterou volam v cyklu trva prumerne 700ms, zatimco kdyz tuto uplne stejnou funkci spustim v thredu (pouze spustim to same v thredu nic nijak neparalelizuju) tak jak je mozne ze bezi cca 2x tak rychleji?