Created
July 18, 2018 10:54
-
-
Save kumailxp/b49e01428fbca58da4a254930370ee16 to your computer and use it in GitHub Desktop.
comparison between using thread vs non-thread and STL accumulate algorithm vs simple for loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<thread> | |
#include <random> | |
#include <chrono> | |
using namespace std; | |
class Timer | |
{ | |
// make things readable | |
using clk = std::chrono::steady_clock; | |
clk::time_point b; // begin | |
clk::time_point e; // end | |
public: | |
void clear() { b = e = clk::now(); } | |
void start() { b = clk::now(); } | |
void stop() { e = clk::now(); } | |
friend std::ostream& operator<<(std::ostream& o, const Timer& timer) | |
{ | |
return o << timer.secs(); | |
} | |
// return time difference in seconds | |
double secs() const | |
{ | |
if(e <= b) | |
return 0.0; | |
auto d = std::chrono::duration_cast<std::chrono::microseconds>(e - b); | |
return d.count() / 1000000.0; | |
} | |
}; | |
#define SIZE 90000000 | |
static void createValueSet(vector<int>& valueSet) { | |
// Seed with a real random value, if available | |
std::random_device r; | |
// Choose a random mean between 1 and 6 | |
std::default_random_engine e1(r()); | |
std::uniform_int_distribution<int> uniform_dist(1, 600); | |
for(int i = 0; i < SIZE; i++) { | |
int mean = uniform_dist(e1); | |
valueSet.push_back(mean); | |
} | |
} | |
static void createValueSetVector(vector<vector<int>> & valueSetVec) { | |
// Seed with a real random value, if available | |
std::random_device r; | |
// Choose a random mean between 1 and 6 | |
std::default_random_engine e1(r()); | |
std::uniform_int_distribution<int> uniform_dist(1, 600); | |
for(auto& I : valueSetVec) { | |
for(int i = 0; i < SIZE; i++) { | |
int mean = uniform_dist(e1); | |
I.push_back(mean); | |
} | |
} | |
} | |
int main () { | |
Timer timer; | |
vector<int> valueSet1; | |
vector<int> valueSet2; | |
vector<int> valueSet3; | |
timer.start(); | |
createValueSet(valueSet1); | |
createValueSet(valueSet2); | |
createValueSet(valueSet3); | |
timer.stop(); | |
cout << "generation time: " << timer << " secs" << endl; | |
cout << "====================================" << endl; | |
valueSet1.clear(); | |
valueSet2.clear(); | |
valueSet3.clear(); | |
timer.start(); | |
thread t4(createValueSet, ref(valueSet1)); | |
thread t5(createValueSet, ref(valueSet2)); | |
thread t6(createValueSet, ref(valueSet3)); | |
t4.join(); | |
t5.join(); | |
t6.join(); | |
timer.stop(); | |
cout << "generation thread time: " << timer << " secs" << endl; | |
cout << "====================================" << endl; | |
valueSet1.empty(); | |
valueSet2.empty(); | |
valueSet3.empty(); | |
timer.start(); | |
vector<vector<int>> valVec = {valueSet1, valueSet2, valueSet3}; | |
createValueSetVector(valVec); | |
timer.stop(); | |
cout << "generation val of vector time: " << timer << " secs" << endl; | |
cout << "====================================" << endl; | |
#if 0 | |
for(auto& I : valueSet) | |
cout << "Val: " << I << endl; | |
#endif | |
timer.start(); | |
long long sum = 0; | |
for(int i = 0 ; i < SIZE; i++) | |
sum += valueSet1.at(i) + valueSet2.at(i) + valueSet3.at(i); | |
timer.stop(); | |
cout << "Loop Sum: " << sum << endl; | |
cout << "time: " << timer << " secs" << endl; | |
cout << "====================================" << endl; | |
sum = 0; | |
timer.start(); | |
for(unsigned int i = 0 ; i < valueSet1.size(); i++) | |
sum += valueSet1.at(i); | |
for(unsigned int i = 0 ; i < valueSet2.size(); i++) | |
sum += valueSet2.at(i); | |
for(unsigned int i = 0 ; i < valueSet3.size(); i++) | |
sum += valueSet3.at(i); | |
timer.stop(); | |
cout << "Loop Sum # 2: " << sum << endl; | |
cout << "time: " << timer << " secs" << endl; | |
cout << "====================================" << endl; | |
sum = 0; | |
timer.start(); | |
sum = accumulate(valueSet1.begin(), valueSet1.end(), sum); | |
sum = accumulate(valueSet2.begin(), valueSet2.end(), sum); | |
sum = accumulate(valueSet3.begin(), valueSet3.end(), sum); | |
timer.stop(); | |
cout << "Acc Sum: " << sum << endl; | |
cout << "time: " << timer << " secs" << endl; | |
cout << "====================================" << endl; | |
sum = 0; | |
timer.start(); | |
long long sum_1 = 0; | |
long long sum_2 = 0; | |
long long sum_3 = 0; | |
thread t1([&valueSet1, &sum_1]() { | |
for(unsigned int i = 0; i < valueSet1.size() ; i++) | |
sum_1 += valueSet1.at(i); | |
}); | |
thread t2([&valueSet2, &sum_2]() { | |
for(unsigned int i = 0; i < valueSet2.size() ; i++) | |
sum_2 += valueSet2.at(i); | |
}); | |
thread t3([&valueSet3, &sum_3]() { | |
for(unsigned int i = 0; i < valueSet3.size() ; i++) | |
sum_3 += valueSet3.at(i); | |
}); | |
t1.join(); | |
t2.join(); | |
t3.join(); | |
sum = sum_1 + sum_2 + sum_3; | |
timer.stop(); | |
cout << "Thread Sum: " << sum << endl; | |
cout << "time: " << timer << " secs" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment