| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2406 人关注过本帖
标题:【有代码有真相】昨天无聊 来了个测试 测试出了一个不好解释的现象 同样代码 ...
只看楼主 加入收藏
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:25 
【有代码有真相】昨天无聊 来了个测试 测试出了一个不好解释的现象 同样代码不同操作系统执行效率不同
linux(64 bit)+firefox 20    617.187 sec

linux(64 bit)+g++ 4.8.0        53.130001 sec
g++ -O3 -msse3 -mfpmath=sse -march=core2 -mtune=core2 -ffast-math
time ./smallpt.out
Rendering (100 spp) 100.00%
53.130001 sec

real    0m53.228s
user    0m53.200s
sys    0m0.007s

linux(64 bit)+g++ 4.8.0+openmp    53.209999 sec
g++ -O3 -msse3 -mfpmath=sse -march=core2 -mtune=core2 -ffast-math -fopenmp
time ./smallpt.out
Rendering (100 spp) 100.00%
53.209999 sec

real    0m26.920s
user    0m53.287s
sys    0m0.007s

XP(32 bit)+firefox 23        517.745 sec

XP(32 bit)+g++ 4.7.2        40.938004 sec
g++ -O3 -msse3 -mfpmath=sse -march=core2 -mtune=core2 -ffast-math
time ./smallpt.exe
Rendering (100 spp) 100.00%
40.938004 sec

real    0m41.172s
user    0m0.015s
sys     0m0.015s

XP(32 bit)+g++ 4.7.2+openmp    19.297001 sec
g++ -O3 -msse3 -mfpmath=sse -march=core2 -mtune=core2 -ffast-math -fopenmp
time ./smallpt.exe
Rendering (100 spp) 100.00%
19.297001 sec

real    0m19.500s
user    0m0.015s
sys     0m0.000s

代码来自这里
http://

问题是 为啥windows下跑的快?我能想到的原因是

1 计时有问题
2 gcc 4.8.0比4.7.2生成的代码慢
3 64 bit环境和32 bit环境的区别
4 windows优秀 linux垃圾 或者说进程调度等的差别
5 代码问题

代码上面的网址可以下载 我再传一份只有js和C++的版本 欢迎大家测试 不过要测试出我这里反映的问题需要同一台电脑装两个操作系统 可能有这个环境的人不多。。

新建文件夹.zip (292.08 KB)



[ 本帖最后由 zklhp 于 2013-4-20 09:21 编辑 ]
搜索更多相关主题的帖子: firefox 测试 
2013-04-20 09:19
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
程序代码:
#include <math.h>   // smallpt, a Path Tracer by Kevin Beason, 2008
#include <stdlib.h> // Make : g++ -O3 -fopenmp smallpt.cpp -o smallpt
#include <stdio.h>  //        Remove "-fopenmp" for g++ version < 4.2
#include <time.h>        

#ifdef __MSC_VER
#define INLINE __forceinline
//#define INLINE __declspec(noinline)
#else
#define INLINE inline
#endif

#ifndef M_PI
#define M_PI 3.141592653589793238462643   
#endif

class RandomLCG {
    unsigned mSeed;

public:
    INLINE RandomLCG(unsigned seed = 0) : mSeed(seed) {
    }

    INLINE double operator()() {
        mSeed = 214013u * mSeed + 2531011u;
        return mSeed * (1.0 / 4294967296.0);
    }
};

struct Vec {
    double x, y, z;

    static const Vec Zero;
    static const Vec XAxis;
    static const Vec YAxis;
    static const Vec ZAxis;

    INLINE Vec(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {
    }

    INLINE Vec operator+(const Vec& b) const {
        return Vec(x + b.x, y + b.y,z + b.z);
    }

    INLINE Vec operator-(const Vec& b) const {
        return Vec(x - b.x, y - b.y, z - b.z);
    }

    INLINE Vec operator*(double b) const {
        return Vec(x * b, y * b, z * b);
    }

    friend INLINE Vec operator*(const Vec& a, const Vec& b) {
        return Vec(a.x * b.x , a.y * b. y, a.z * b.z);
    }

    INLINE Vec norm() const {
        return *this * (1.0 / sqrt(x * x + y * y + z * z));
    }

    INLINE double operator%(const Vec& b) const { // dot
        return x * b.x + y * b.y + z * b.z;
    }

    INLINE Vec operator^(const Vec& b) const { // cross
        return Vec(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x);
    }
};

const Vec Vec::Zero(0, 0, 0);
const Vec Vec::XAxis(1, 0, 0);
const Vec Vec::YAxis(0, 1, 0);
const Vec Vec::ZAxis(0, 0, 1);

struct Ray {
    Vec o, d;

    INLINE Ray(const Vec &o, const Vec &d) : o(o), d(d) {
    }
};

// material types, used in radiance()
enum Refl_t {
    DIFF,
    SPEC,
    REFR
};

struct Sphere {
    Vec p, e, c;      // position, emission, color
    Vec cc;
    double rad;       // radius
    double sqRad;
    double maxC;
    Refl_t refl;      // reflection type (DIFFuse, SPECular, REFRactive)

    Sphere(double rad, const Vec& p, const Vec& e, const Vec& c, Refl_t refl) : p(p), e(e), c(c), rad(rad), refl(refl) {
        sqRad = rad * rad;
        maxC = c.x > c.y && c.y > c.z ? c.x : c.y > c.z ? c.y : c.z;
        cc = c * (1.0 / maxC);
    }

    // returns distance, 1e20 if nohit
    INLINE double intersect(const Ray &r) const    {
        // Solve t^2*d.d + 2*t*(o-p).d + (o-p).(o-p)-R^2 = 0
        Vec op = p - r.o;
        double b = op % r.d;
        double det = b * b - op % op + sqRad;
        const double eps = 1e-4;

        if (det < 0)
            return 1e20;
        else {
            double dets = sqrt(det);

            if (b - dets > eps)
                return b - dets;
            else if (b + dets > eps)
                return b + dets;
            else
                return 1e20;
        }
    }
};

//Scene: radius, position, emission, color, material
static Sphere spheres[] = {
    Sphere(1e5,  Vec( 1e5+1,40.8,81.6),  Vec::Zero, Vec(.75,.25,.25), DIFF),//Left
    Sphere(1e5,  Vec(-1e5+99,40.8,81.6), Vec::Zero, Vec(.25,.25,.75), DIFF),//Rght
    Sphere(1e5,  Vec(50,40.8, 1e5),      Vec::Zero, Vec(.75,.75,.75), DIFF),//Back
    Sphere(1e5,  Vec(50,40.8,-1e5+170),  Vec::Zero, Vec::Zero,        DIFF),//Frnt
    Sphere(1e5,  Vec(50, 1e5, 81.6),     Vec::Zero, Vec(.75,.75,.75), DIFF),//Botm
    Sphere(1e5,  Vec(50,-1e5+81.6,81.6), Vec::Zero, Vec(.75,.75,.75), DIFF),//Top
    Sphere(16.5, Vec(27,16.5,47),        Vec::Zero, Vec(1,1,1)*.999,  SPEC),//Mirr
    Sphere(16.5, Vec(73,16.5,78),        Vec::Zero, Vec(1,1,1)*.999,  REFR),//Glas
    Sphere(600,  Vec(50,681.6-.27,81.6), Vec(12,12,12), Vec::Zero,    DIFF) //Lite
};

INLINE double clamp(double x) {
    if (x < 0)
        return 0;
    else if (x > 1)
        return 1;
    else
        return x;
}

INLINE int toInt(double x) {
    return int(pow(clamp(x), 1 / 2.2) * 255 + .5);
}

INLINE Sphere* intersect(const Ray &r, double &t) {
    t = 1e20;
    Sphere* ret = NULL;

    for (Sphere* s = spheres; s != spheres + sizeof(spheres) / sizeof(Sphere); ++s) {
        double d = s->intersect(r);
        if (d < t) {
            t = d;
            ret = s;
        }
    }
    return ret;
}

static Vec radiance(const Ray &r, int depth, RandomLCG& rand) {
    double t;                               // distance to intersection
    Sphere* obj = intersect(r, t);

    if (!obj)
        return Vec::Zero; // if miss, return black
    else {
        int newDepth = depth + 1;
        bool isMaxDepth = newDepth > 100;

        // Russian roulette for path termination
        bool isUseRR = newDepth > 5;
        bool isRR = isUseRR && rand() < obj->maxC;

        if (isMaxDepth || (isUseRR && !isRR))
            return obj->e;
        else {
            Vec f = (isUseRR && isRR) ? obj->cc : obj->c;
            Vec x = r.o + r.d * t;
            Vec n = (x - obj->p).norm();
            Vec nl = n % r.d < 0 ? n : n * -1;

            if (obj->refl == DIFF) { // Ideal DIFFUSE reflection
                double r1 = 2 * M_PI * rand();
                double r2 = rand();
                double r2s = sqrt(r2);

                Vec w = nl;
                Vec wo = w.x < -0.1 || w.x > 0.1 ? Vec::YAxis : Vec::XAxis;
                Vec u = (wo ^ w).norm();
                Vec v = w ^ u;

                Vec d = (u * cos(r1) * r2s + v * sin(r1) * r2s + w * sqrt(1 - r2)).norm();

                return obj->e + f * radiance(Ray(x, d), newDepth, rand);
            }
            else if (obj->refl == SPEC) // Ideal SPECULAR reflection
                return obj->e + f * radiance(Ray(x, r.d - n * (2 * (n % r.d))), newDepth, rand);
            else { // Ideal dielectric REFRACTION
                Ray reflRay(x, r.d - n * (2 * (n % r.d)));
                bool into = n % nl > 0;  // Ray from outside going in?
                double nc = 1;
                double nt = 1.5;
                double nnt = into ? nc / nt : nt / nc;
                double ddn = r.d % nl;
                double cos2t = 1 - nnt * nnt * (1 - ddn * ddn);

                if (cos2t < 0)  // Total internal reflection
                    return obj->e + f * radiance(reflRay, newDepth, rand);
                else
                {
                    Vec tdir = (r.d * nnt - n * ((into ? 1 : -1) * (ddn * nnt + sqrt(cos2t)))).norm();
                    double a = nt - nc;
                    double b = nt + nc;
                    double R0 = (a * a) / (b * b);
                    double c = 1 - (into ? -ddn : tdir % n);
                    double Re = R0 + (1 - R0) * c * c * c * c * c;
                    double Tr = 1 - Re;
                    double P = .25 + .5 * Re;
                    double RP = Re / P;
                    double TP = Tr / (1 - P);

                    Vec result;
                    if (newDepth > 2) {
                        // Russian roulette and splitting for selecting reflection and/or refraction
                        if (rand() < P)
                            result = radiance(reflRay, newDepth, rand) * RP;
                        else
                            result = radiance(Ray(x, tdir), newDepth, rand) * TP;
                    }
                    else
                        result = radiance(reflRay, newDepth, rand) * Re + radiance(Ray(x, tdir), newDepth, rand) * Tr;

                    return obj->e + f * result;
                }
            }
        }
    }
}

int main(int argc, char *argv[]) {
    clock_t start = clock();

    const int w = 256;
    const int h = 256;

    const int samps = argc == 2 ? atoi(argv[1]) / 4 : 25; // # samples

    const Ray cam(Vec(50, 52, 295.6), Vec(0, -0.042612, -1).norm()); // cam pos, dir
    const Vec cx(w * .5135 / h);
    const Vec cy = (cx ^ cam.d).norm() * .5135;
    Vec* c = new Vec[w * h];

#pragma omp parallel for schedule(dynamic, 1)       // OpenMP
    // Loop over image rows
    for (int y = 0; y < h; y++) {
        fprintf(stderr,"\rRendering (%d spp) %5.2f%%",samps*4,100.*y/(h-1));
        RandomLCG rand(y);

        // Loop cols
        for (unsigned short x=0; x<w; x++) {
            // 2x2 subpixel rows
            for (int sy = 0; sy < 2; sy++) {
                const int i = (h - y - 1) * w + x;

                // 2x2 subpixel cols
                for (int sx = 0; sx < 2; sx++) {       
                    Vec r = Vec::Zero;
                    for (int s = 0; s < samps; s++) {
                        double r1 = 2 * rand();
                        double r2 = 2 * rand();
                        double dx = r1 < 1 ? sqrt(r1) - 1 : 1 - sqrt(2 - r1);
                        double dy = r2 < 1 ? sqrt(r2) - 1 : 1 - sqrt(2 - r2);

                        Vec d = cx * (((sx + .5 + dx) / 2 + x) / w - .5) +
                                cy * (((sy + .5 + dy) / 2 + y) / h - .5) + cam.d;

                        r = r + radiance(Ray(cam.o + d * 140, d.norm()), 0, rand) * (1.0 / samps);
                    }
                    c[i] = c[i] + Vec(clamp(r.x), clamp(r.y), clamp(r.z)) * .25;
                }
            }
        }
    }

    printf("\n%f sec\n", (float)(clock() - start) / CLOCKS_PER_SEC);

    FILE *f = fopen("image.ppm", "w"); // Write image to PPM file.
    fprintf(f, "P3\n%d %d\n%d\n", w, h, 255);
    for (int i = 0; i < w * h; i++)
        fprintf(f,"%d %d %d\n", toInt(c[i].x), toInt(c[i].y), toInt(c[i].z));
    fclose(f);
}



C++代码是这样的 也贴一下罢
2013-04-20 09:20
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
当然这里的测试结果有一个积极的地方 目前firefox的js引擎可以做到在密集计算中比本地代码只慢大约一个数量级 虽然js恐怕没法做到并行计算 这个还是有一定的意义的
2013-04-20 09:28
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:17 
这个WinInter联盟不是一般人想的这么简单.
Windows内核代码运行效率肯定还是有独到之处...要不就没有二者之争了.

我们都在路上。。。。。
2013-04-20 09:32
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
gcc 4.8.0比4.7.2生成的代码慢?这个有证据否?

我们都在路上。。。。。
2013-04-20 09:34
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
以下是引用hellovfp在2013-4-20 09:34:27的发言:

gcc 4.8.0比4.7.2生成的代码慢?这个有证据否?
我瞎说的 但从4.8开始好像改模块化了还是神马 但不至于造成性能大幅下降罢

至于windows内核的优秀 这个罢 一个用户模式的程序能体现出来么 有的话 也是进程调度的策略罢

比较可能的原因是 一个测试环境是64位 一个是32位 代码行为不同 可惜C++的代码 我也看不大懂。。
2013-04-20 09:40
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
还有就是linux的64位操作系统,是否成熟?...有没有和windows 64位的进行过比较?
光看32位对比,可能出不来结果....可惜的是,偶一直是32的...有机会换机子,也玩一把64位的.

我们都在路上。。。。。
2013-04-20 09:43
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:17 
看看
2013-04-20 09:45
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
以下是引用hellovfp在2013-4-20 09:43:15的发言:

还有就是linux的64位操作系统,是否成熟?...有没有和windows 64位的进行过比较?
光看32位对比,可能出不来结果....可惜的是,偶一直是32的...有机会换机子,也玩一把64位的.

一般来说 换了64位跑分神马的能快一点 但快不多

写个简单的程序看看循环效率两个系统有没有区别。。
2013-04-20 09:46
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
让T版 的64位机器来跑跑..可能结果出人意外...Win居然比linux的慢.....让人大跌眼镜啊...

我们都在路上。。。。。
2013-04-20 09:52
快速回复:【有代码有真相】昨天无聊 来了个测试 测试出了一个不好解释的现象 同 ...
数据加载中...
 
   



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

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