好丢人啊 改改编译参数手工汇编优化的代码就被甩出好多 改天认真研究一下编译器的写法 好像我的写法有点问题
先上结论C Elapsed 2218 ms
C(with complex) Elapsed 2125 ms
ASM Elapsed 2156 ms
程序 欢迎测试
c+c_99+asm.zip
(17.52 KB)
两个C代码依次是
程序代码:
// 代码来自 <span style="color: #008000; text-decoration: underline;">https://bbs.bccn.net/thread-365456-1-1.html[/color] // gcc -Wall -O3 fenxing.c -msse3 -march=core2 -mfpmath=sse -ffast-math -o c.exe #include <stdio.h> #include <time.h> #include <stdlib.h> #define BAILOUT 16 #define MAX_ITERATIONS 1000 #define SIZE (1024*1024*1) char *pnow = NULL; inline int put(char ch) { *pnow++ = ch; return 0; } inline int mandelbrot(double x, double y) { double cr = y - 0.5; double ci = x; double zi = 0.0; double zr = 0.0; int i = 0; while(1) { i++; double temp = zr * zi; double zr2 = zr * zr; double zi2 = zi * zi; zr = zr2 - zi2 + cr; zi = temp + temp + ci; if (zi2 + zr2 > BAILOUT) return i; if (i > MAX_ITERATIONS) return 0; } } int main() { long t1 = 0, t2 = 0; char *pbuf = calloc(1, SIZE); pnow = pbuf; t1 = clock(); int x,y; for (y = -399; y < 399; y++) { put('\n'); for (x = -399; x < 399; x++) { int i = mandelbrot(x/400.0, y/400.0); if (i == 0) put('*'); else put(' '); } } put('\n'); t2 = clock(); printf("C Elapsed %ld ms\n", (t2 - t1)); printf("%s\n", pbuf); free(pbuf); return 0; }
程序代码:
// 代码来自 <span style="color: #008000; text-decoration: underline;">https://bbs.bccn.net/thread-365456-1-1.html[/color] // gcc -Wall fenxing_C99.c -O3 -msse3 -march=core2 -mfpmath=sse -ffast-math -o c_c99.exe #include <stdio.h> #include <time.h> #include <stdlib.h> #include <complex.h> #define BAILOUT 16 #define MAX_ITERATIONS 1000 #define SIZE (1024*1024*10) char *pnow = NULL; inline void my_put(char ch) { *pnow++ = ch; } inline int mandelbrot(double x, double y) { double _Complex c = y - 0.5 + x * I; double _Complex z = 0.0; int i = 0; while(1) { i++; if (creal(z)*creal(z)+cimag(z)*cimag(z) > BAILOUT) return i; if (i > MAX_ITERATIONS) return 0; z = z * z + c; } } int main() { long t1 = 0, t2 = 0; char *pbuf = calloc(1, SIZE); pnow = pbuf; t1 = clock(); int x,y; for (y = -399; y < 399; y++) { my_put('\n'); for (x = -399; x < 399; x++) { if (mandelbrot(x/400.0, y/400.0) == 0) my_put('*'); else my_put(' '); } } my_put('\n'); t2 = clock(); printf("C(with complex) Elapsed %ld ms\n", (t2 - t1)); printf("%s\n", pbuf); free(pbuf); return 0; }
一个收获是 写直接的代码 这里采用C里面带的复数的特性就比自己实现的复数运算效率要高 因为这时候优化的程序更容易理解编程者的意思 还有一个收获是 我的SSE水平有待提高啊 呵呵