Created
June 7, 2013 02:11
-
-
Save boutell/5726634 to your computer and use it in GitHub Desktop.
As part of testing the performance of my asm.js mandelbrot set renderer, I've backported it to C in order to see what performance is like there. If you are not careful gcc or clang will optimize away the whole thing and complete in seemingly zero time because it knows you're not really using the computed data. I found that passing the image data…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
#include <stdio.h> | |
#include <sys/time.h> | |
double microtime(); | |
// C version of bench from Bug 879891. 1 iteration completes in 0.17 seconds | |
void compute(int *image); | |
int main() { | |
// pass the image buffer into compute as a pointer. Otherwise | |
// gcc is able to figure out the image data is never really used, | |
// and it takes zero time because it optimizes away the entire | |
// computation! The time comes back if you (1) do this, (2) | |
// output stuff, or (3) just count operations performed | |
int image[1120000]; | |
compute(image); | |
} | |
void compute(int *image) { | |
double cx = -0.75; | |
double cy = 0.0; | |
double to = 0.0; | |
double m = 1.0; | |
double y = 0.0; | |
double x = 0.0; | |
double ox = 0.0; | |
double oy = 0.0; | |
double zx = 0.0; | |
double zy = 0.0; | |
double xt = 0.0; | |
int j = 0; | |
int i = 0; | |
int p = 0; | |
double start, end; | |
start = microtime(); | |
for (j = 0; (j < 1); j++) { | |
i = 0; | |
p = 0; | |
y = 0.0; | |
while (y < 400.0) { | |
x = 0.0; | |
while (x < 700.0) { | |
ox = m * (x / 200.0 - 1.75) + cx; | |
oy = m * (y / 200.0 - 1.0) + cy; | |
zx = 0.0; | |
zy = 0.0; | |
i = 0; | |
while (1) { | |
if (zx*zx + zy*zy >= 4.0) { | |
break; | |
} | |
if ((i|0) >= 767) { | |
break; | |
} | |
xt = zx*zx - zy*zy + ox; | |
zy = 2.0*zx*zy + oy; | |
zx = xt; | |
i++; | |
} | |
// if (i == 767) { | |
// printf("*"); | |
// } else { | |
// printf(" "); | |
// } | |
image[p] = i; | |
x = x + 1.0; | |
p++; | |
} | |
// printf("\n"); | |
y = y + 1.0; | |
} | |
} | |
end = microtime(); | |
printf("%.10f\n", end - start); | |
// printf("passes: %f\n", count); | |
} | |
double microtime() { | |
double m = 0.0; | |
struct timeval time; | |
gettimeofday(&time, NULL); | |
m = time.tv_sec; | |
m += time.tv_usec/1000000.0; // microseconds; | |
return m; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment