Last active
January 13, 2018 20:37
-
-
Save DavidButts/6aff99e8477bf13556d2ef1a64803c5f 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
//Written by David Butts | |
#include<time.h> | |
#include<chrono> | |
#include<thread> | |
#include<iomanip> | |
int next_val(const vector<int> &v){ | |
if (v == vector<int>{0,0,0}) | |
return 0; | |
else if (v == vector<int>{0,0,1}) | |
return 0; | |
else if (v == vector<int>{0,1,0}) | |
return 0; | |
else if (v == vector<int>{0,1,1}) | |
return 1; | |
else if (v == vector<int>{1,0,0}) | |
return 1; | |
else if (v == vector<int>{1,0,1}) | |
return 1; | |
else if (v == vector<int>{1,1,0}) | |
return 1; | |
else | |
return 0; | |
} | |
vector<int> one_iteration(const vector<int> &v){ | |
vector<int> vec = v; | |
for(int i = 1; i < v.size() - 1; i++) | |
vec[i] = next_val({v[i-1],v[i],v[i+1]}); | |
vec[0] = next_val({v[v.size()-1],v[0],v[1]}); | |
vec[v.size()-1] = next_val({v[v.size() - 2],v[v.size() - 1],v[0]}); | |
return vec; | |
} | |
int main() { | |
cout << std::fixed << std::setprecision(5); | |
int iter = 1000; | |
long av_time = 0; | |
std::vector<int> v(100000,0); | |
v[50] = 1; | |
for(int run = 0; run < 10; run++){ | |
auto t_start = std::chrono::high_resolution_clock::now(); | |
for(int i=0; i<iter; ++i){ | |
v = one_iteration(v); | |
} | |
auto t_end = std::chrono::high_resolution_clock::now(); | |
auto duration = std::chrono::duration<double, std::milli>(t_end-t_start); | |
av_time += duration.count(); | |
} | |
cout << "msec = " << av_time/10.0 << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment