aboutsummaryrefslogtreecommitdiff
path: root/Test - Benchmark/Mandelbrot.c
blob: d06d154c03586f53428e2dc1860ea0d4325bad4d (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>

#define x_MIN -2.0
#define x_MAX  1.0
#define y_MIN -1.0
#define y_MAX  1.0

#define BMP_HEIGHT 800
#define BMP_WIDTH 1000

#define N_MAX 250

void toMath(int X, int Y, double* x, double* y) {
  if ((X < 0) || (X > BMP_WIDTH) || (Y < 0) || (Y > BMP_HEIGHT)) {
    printf("Fehler: 0 <= %d <= %d\n", X, BMP_WIDTH);
    printf("   oder 0 <= %d <= %d verletzt!\n", Y, BMP_HEIGHT);
    *x = x_MIN;
    *y = y_MIN;
    return;
  }
  *x = x_MIN + ((double) X * (x_MAX - x_MIN)) / BMP_WIDTH;
  *y = y_MIN + ((double) (BMP_HEIGHT - Y) * (y_MAX - y_MIN)) / BMP_HEIGHT;
}

int mandelbrot(double x, double y, double zx, double zy, int n, int n_max,
               double threshold) {
  if ((n <= n_max) && ((zx * zx + zy * zy) < threshold)) {
    double zx_new = (zx * zx - zy * zy + x);
    double zy_new = (2 * zx * zy + y);
    if ((zx_new == zx) && (zy_new == zy)) {
      return n_max;
    }
    n = mandelbrot(x, y, zx_new, zy_new, n + 1, n_max, threshold);
  }
  return n;
}


int main(){
	
	float z;
	clock_t begin, begin2, end, end2;
	uint32_t X = 0, Y = 0;
	
	begin = clock();
	uint32_t* data = (uint32_t*) malloc(BMP_WIDTH * BMP_HEIGHT * sizeof(uint32_t));
	int n = 0;
	double x = 0.0, y = 0.0;
	int n_max = 500;      
	double threshold = 4.0; 

	for (int Y = 0; Y < BMP_HEIGHT; Y++) {
		for (int X = 0; X < BMP_WIDTH; X++) {
		  double x, y;
		  toMath(X, Y, &x, &y);
		  int n = mandelbrot(x, y, 0.0, 0.0, 0, n_max, threshold);
		  data[Y * BMP_WIDTH + X] = n;
		}
	}
	end = clock();
	// printf("Rekursion Ende");
	// getchar();
	
	// for(int i = 0; i < BMP_HEIGHT*BMP_WIDTH; i++){
		// printf("%d\n",data[i]);
	// }

	begin2 = clock();
	uint32_t* data2 = (uint32_t*) malloc(BMP_WIDTH * BMP_HEIGHT * sizeof(uint32_t));
	int m = 0;

	for (int Y = 0; Y < BMP_HEIGHT; Y++) {
		for (int X = 0; X < BMP_WIDTH; X++) {
			double x, y, x2;
			toMath(X, Y, &x, &y);
			double cx = x;
			double cy = y;
			while(m <=n_max && (x*x)+(y*y) <= 4){
				x2 = x;
				x = (x*x) - (y*y) + cx;
				y = 2.0*x2*y + cy;
				m++;
			}
			data2[Y * BMP_WIDTH + X] = n;
		}
	}
	end2 = clock();
	
	// for(int i = 0; i < BMP_HEIGHT*BMP_WIDTH; i++){
		// printf("%d\n",data2[i]);
	// }

	
	printf("Rekursion:\n");
	printf("begin:                         %d\n", begin);
	printf("end:                           %d\n", end);
	printf("clocks zwischen begin und end: %d\n", end - begin);
	z=end - begin;
	z/=CLOCKS_PER_SEC;
	printf("Zeit zwischen begin und end:   %f Sekunden\n", z);
	printf("CLOCKS_PER_SEC:                %d\n", CLOCKS_PER_SEC);
	
	printf("Schleifen:\n");
	printf("begin:                         %d\n", begin2);
	printf("end:                           %d\n", end2);
	printf("clocks zwischen begin und end: %d\n", end2 - begin2);
	z=end2 - begin2;
	z/=CLOCKS_PER_SEC;
	printf("Zeit zwischen begin und end:   %f Sekunden\n", z);
	printf("CLOCKS_PER_SEC:                %d\n", CLOCKS_PER_SEC);



}