-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconway.c
More file actions
71 lines (56 loc) · 1.4 KB
/
conway.c
File metadata and controls
71 lines (56 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#define size 1024
#define gens 10
bool cells[size][size];
bool new_cells[size][size];
void randomizeCells() {
for (int x = 0; x < size; ++x) {
for (int y = 0; y < size; ++y) {
cells[x][y] = rand() % 2 == 0;
}
}
}
void printCells() {
for (int x = 0; x < size; ++x) {
for (int y = 0; y < size; ++y) {
printf(cells[x][y] ? "o" : " ");
}
printf("\n");
}
}
void nextGeneration() {
for (int x = 0; x < size; ++x) {
for (int y = 0; y < size; ++y) {
int neighbors = 0;
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
int nx = (size + x + dx) % size;
int ny = (size + y + dy) % size;
if ((dx != 0 || dy != 0) && cells[nx][ny]) {
neighbors++;
}
}
}
new_cells[x][y] = neighbors == 3 || (neighbors == 2 && cells[x][y]);
}
}
memcpy(cells, new_cells, sizeof cells);
}
int main(void) {
randomizeCells();
struct timeval start, stop;
gettimeofday(&start, NULL);
for (int i = 0; i < gens; ++i) {
nextGeneration();
}
gettimeofday(&stop, NULL);
float seconds = (stop.tv_usec - start.tv_usec) / 1e6 + stop.tv_sec - start.tv_sec;
int ops = size * size * gens;
printf("C Efficiency in cellhz: %e\n", 1.0 * ops / seconds);
return 0;
}