aboutsummaryrefslogtreecommitdiff
path: root/Test - Benchmark/Mandelbrot.c
diff options
context:
space:
mode:
authorJNDTUHH <jndiercks@gmail.com>2018-01-14 15:09:59 +0100
committerJNDTUHH <jndiercks@gmail.com>2018-01-14 15:09:59 +0100
commit026d33d12150bdf14c4ecd6b4dc359182d434131 (patch)
tree27c670d1ab5f7d2eabda87fa4f7f4215aa158e21 /Test - Benchmark/Mandelbrot.c
parente9b8b9b9ac126abe75c1b6cbafb4e67d0a44b31e (diff)
Einfache Laufzeitmessung Rekursion/Schleife
Diffstat (limited to 'Test - Benchmark/Mandelbrot.c')
-rw-r--r--Test - Benchmark/Mandelbrot.c120
1 files changed, 120 insertions, 0 deletions
diff --git a/Test - Benchmark/Mandelbrot.c b/Test - Benchmark/Mandelbrot.c
new file mode 100644
index 0000000..eecb7cb
--- /dev/null
+++ b/Test - Benchmark/Mandelbrot.c
@@ -0,0 +1,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);
+
+
+
+} \ No newline at end of file