#include <iostream>
#include <vector>
#include <sys/time.h>
#include <math.h>
using namespace std;
int main(int argc, char **argv) {
struct timeval start, end;
vector<vector<double> > a(10000, vector<double>(10000));
gettimeofday(&start, NULL);
for(int i = 0; i < 10000; ++i) {
for(int j = 0; j < 10000; ++j) {
a[i][j] = cos(1.05);
}
}
gettimeofday(&end, NULL);
float cost_time = (end.tv_usec-start.tv_usec)/1000000.0 + end.tv_sec-start.tv_sec;
cout << cost_time << endl;
cout << a[666][6] << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <sys/time.h>
#include <math.h>
#include <thread>
using namespace std;
void parallel(vector<vector<double> > &a, int index){
for(int i = index; i < index+2000; ++i) {
for(int j = 0; j < 10000; ++j) {
a[i][j] = cos(1.05);
}
}
}
int main(int argc, char **argv) {
struct timeval start, end;
vector<vector<double> > a(10000, vector<double>(10000));
gettimeofday(&start, NULL);
thread t1(parallel, std::ref(a), 0);
thread t2(parallel, std::ref(a), 2000);
thread t3(parallel, std::ref(a), 4000);
thread t4(parallel, std::ref(a), 6000);
thread t5(parallel, std::ref(a), 8000);
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
gettimeofday(&end, NULL);
float cost_time = (end.tv_usec-start.tv_usec)/1000000.0 + end.tv_sec-start.tv_sec;
cout << cost_time << endl;
cout << a[666][6] << endl;
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)