Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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