-
-
Save Dobiasd/839acc2bc7a1f48a5063 to your computer and use it in GitHub Desktop.
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
// compile with | |
// g++ -o main -O3 -std=c++11 -Wall -Wextra -pedantic -Werror main.cpp | |
// | |
// run: | |
// main 1 | |
// main 2 | |
// main 3 | |
// main 4 | |
// main 5 | |
// main 6 | |
#include <algorithm> | |
#include <chrono> | |
#include <ctime> | |
#include <functional> | |
#include <iostream> | |
#include <iterator> | |
#include <numeric> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
vector<int> squareVec1(vector<int> v) | |
{ | |
auto it = begin(v); | |
loopBegin: | |
if (it == end(v)) | |
goto loopEnd; | |
*it = *it * *it; | |
++it; | |
goto loopBegin; | |
loopEnd: | |
return v; | |
} | |
vector<int> squareVec2(vector<int> v) | |
{ | |
auto it = begin(v); | |
while (it != end(v)) | |
{ | |
*it = *it * *it; | |
++it; | |
} | |
return v; | |
} | |
vector<int> squareVec3(vector<int> v) | |
{ | |
for (auto it = begin(v); it != end(v); ++it) | |
{ | |
*it = *it * *it; | |
} | |
return v; | |
} | |
vector<int> squareVec4(vector<int> v) | |
{ | |
for (int& i : v) | |
{ | |
i = i * i; | |
} | |
return v; | |
} | |
vector<int> squareVec5(vector<int> v) | |
{ | |
transform(begin(v), end(v), begin(v), [](int i) | |
{ | |
return i*i; | |
}); | |
return v; | |
} | |
template<typename Container, typename Functor> | |
Container transformCont(Container xs, Functor op) | |
{ | |
transform(begin(xs), end(xs), begin(xs), op); | |
return xs; | |
} | |
vector<int> squareVec6(const vector<int>& v) | |
{ | |
return transformCont(v, [](int i) | |
{ | |
return i*i; | |
}); | |
} | |
vector<int> squareVec7(const vector<int>& v) | |
{ | |
vector<int> result; | |
result.reserve(v.size()); | |
return accumulate(begin(v), end(v), result, | |
[](vector<int>& acc, int i) | |
{ | |
acc.push_back(i*i); | |
return acc; | |
}); | |
} | |
vector<int> squareVecNTimes(vector<int> v, int n, | |
const string& version) | |
{ | |
function<vector<int>(const vector<int>&)> f; | |
if (version=="1") f = squareVec1; | |
else if (version=="2") f = squareVec2; | |
else if (version=="3") f = squareVec3; | |
else if (version=="4") f = squareVec4; | |
else if (version=="5") f = squareVec5; | |
else if (version=="6") f = squareVec6; | |
else if (version=="7") f = squareVec7; | |
else return v; | |
vector<int> v2; | |
for (int i = 0; i < n; ++i) | |
{ | |
v[i % v.size()] = i; | |
v2 = f(v); | |
} | |
return v2; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (argc < 2) | |
return 1; | |
vector<int> v(10000); | |
generate(begin(v), end(v), rand); | |
typedef chrono::time_point<std::chrono::system_clock> Time; | |
Time startTime = chrono::system_clock::now(); | |
vector<int> v2 = squareVecNTimes(v, 50000, argv[1]); | |
Time endTime = chrono::system_clock::now(); | |
chrono::duration<double> elapsed_seconds = endTime - startTime; | |
cout << argv[1] << ": " | |
<< accumulate(begin(v2), end(v2), 0) | |
<< " - elapsed time: " << elapsed_seconds.count() << "s\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment