标题:好丢人啊 改改编译参数手工汇编优化的代码就被甩出好多 改天认真研究一下编 ...
只看楼主
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
结帖率:100%
 问题点数:0 回复次数:2 
好丢人啊 改改编译参数手工汇编优化的代码就被甩出好多 改天认真研究一下编译器的写法 好像我的写法有点问题
先上结论

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水平有待提高啊 呵呵

搜索更多相关主题的帖子: 手工 complex 编译器 color 
2012-12-09 20:51
wp231957
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:422
帖 子:13681
专家分:53296
注 册:2012-10-18
得分:0 
楼主威武

DO IT YOURSELF !
2012-12-09 20:59
suijishu
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:66
专家分:144
注 册:2012-12-1
得分:0 
膜拜大牛!!!膜拜大作!!!
2012-12-09 23:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-390346-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.670527 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved