Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include<stdio.h>
#include<stdlib.h>
#include <sys/time.h>
#include <math.h>
#include<time.h>
#include <chrono>
#include <iostream>
#define BOOL(x) (!(!(x)))
#define BitSet(arg,posn) ((arg) | (1L << (posn)))
#define BitClr(arg,posn) ((arg) & ~(1L << (posn)))
#define BitTst(arg,posn) BOOL((arg) & (1L << (posn)))
#define BitFlp(arg,posn) ((arg) ^ (1L << (posn)))
// ## NUMERO DE INSTRUCOES
const int NUM_ELEM = 512;
//const int sides = 2;
int regra = 30;
using namespace std;
int calculaCentro(int centro, char elem[])
{
//Vars de controle
int num = 0;
int res = 0;
//printf("rule: %i\n", regra);
for(int x = centro-2/2,y=2;x <= centro+2/2;x++,y--)
{
num = BitClr(num, y);
if(elem[(x % NUM_ELEM + NUM_ELEM) %NUM_ELEM])
num = BitSet(num, y);
}
res = BitTst(regra,num);
//printf("Num: %i\n", num);
//printf("Bit Central: %i\n", res);
return res;
}
int main()
{
//Definir elementos do array de bits
char elem[NUM_ELEM];
char welem[NUM_ELEM];
for(int x=0; x<=NUM_ELEM;x++)
{
elem[x] = 0;
}
elem[0] = 1;
int MAXITER = 100000;
int ITER = 0;
auto begin = std::chrono::high_resolution_clock::now();
while(ITER++<=MAXITER)
{
for(int x=0; x<=NUM_ELEM;x++)
{
/////////////////////////////////////////////////////////// TEMPORIZAR ISSO AQUi
welem[x] = calculaCentro(x,elem);
}
for(int x=0; x<=NUM_ELEM;x++)
{
//printf("%i", welem[x]);
elem[x] = welem[x];
}
//printf("\n");
//getchar();
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Regra: " << regra << std::endl;
std::cout << "Iteracoes: " << MAXITER << std::endl;
std::cout << "Celulas: " << NUM_ELEM << std::endl;
std::cout << "Total: " << std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count() << "ns" << std::endl;
}
/*
0, 0, 0, 0, 0 = 0
0, 0, 0, 0, 1 = 1
0, 0, 0, 1, 0 = 1
0, 0, 0, 1, 1 = 1
0, 0, 1, 0, 0 = 1
0, 0, 1, 0, 1 = 0
0, 0, 1, 1, 0 = 0
0, 0, 1, 1, 1 = 0
0, 1, 0, 0, 0 = 0
0, 1, 0, 0, 1 = 1
0, 1, 0, 1, 0 = 1
0, 1, 0, 1, 1 = 1
0, 1, 1, 0, 0 = 1
0, 1, 1, 0, 1 = 0
0, 1, 1, 1, 0 = 0
0, 1, 1, 1, 1 = 0
1, 0, 0, 0, 0 = 0
1, 0, 0, 0, 1 = 1
1, 0, 0, 1, 0 = 1
1, 0, 0, 1, 1 = 1
1, 0, 1, 0, 0 = 1
1, 0, 1, 0, 1 = 0
1, 0, 1, 1, 0 = 0
1, 0, 1, 1, 1 = 0
1, 1, 0, 0, 0 = 0
1, 1, 0, 0, 1 = 1
1, 1, 0, 1, 0 = 1
1, 1, 0, 1, 1 = 1
1, 1, 1, 0, 0 = 1
1, 1, 1, 0, 1 = 0
1, 1, 1, 1, 0 = 0
1, 1, 1, 1, 1 = 0
*/
@BernardoGO

This comment has been minimized.

Copy link
Owner Author

commented Jan 23, 2018

Regra: 30
Iteracoes: 100000
Celulas: 512
Total: 9.112.649.543ns

Regra: 30
Iteracoes: 100000
Celulas: 256
Total: 4.451.179.527ns

Regra: 30
Iteracoes: 100000
Celulas: 128
Total: 2.246.534.615ns

Regra: 30
Iteracoes: 100000
Celulas: 64
Total: 1.163.697.245ns

Regra: 30
Iteracoes: 100000
Celulas: 32
Total: 623.358.976ns

Regra: 30
Iteracoes: 100000
Celulas: 16
Total: 346.715.629ns

Regra: 30
Iteracoes: 100000
Celulas: 8
Total: 198.324.095ns

Regra: 30
Iteracoes: 1
Celulas: 8
Total: 9166ns

Regra: 30
Iteracoes: 10
Celulas: 8
Total: 40937ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.