c++代码优化,运行时间目前是9.8s(必须在gcc上运行),目标是6.5s以内,现在遇到瓶颈,如能解答报酬好说

#include
#include
#include
#include
using namespace std;

int main(int argc, char* argv[]) {

static const int nx(10000);

static const int ny(200);

static const int nt(200);
int i(0), j(0);

double* vi = new double[nx*ny];

double* vr = new double[nx*ny];

static const double pi = (4.*atan(1.) / double(nx));
memset(vr, 0, ny*nx * sizeof(double));
int iny((nx-1)*ny);
for (i = nx-1; i>=0; --i) {

    for (j = ny-1; j>=0; --j) {
        vi[iny + j] = double(i*i)*double(j)*sin(pi *double(i));
    }
    iny -= ny;
}


ofstream fout("data_out");
FILE *fp = fopen("data_out1", "w");
/*
ÏÂÃæµÄÑ­»·Àï³öÏÖÁË·ÖÖ§£¬¸ù¾ÝÀÏʦPPT£¬½«ÕâÀïµÄÑ­»·²ð·Ö³ÉÈýÏ¼õÉÙÁËÑ­»·´ÎÊý¡£
i,j±»Öظ´¶¨ÒåÁËÈý´Î£¬ÔÚÿ´ÎÑ­»·ÖÐ
*/

for (int t = 0; t<nt; t++) {
    cout << "\n" << t; cout.flush();


    iny = ny;
    for (i = 1; i < nx - 1; ++i) {
        for (j = 1; j < ny - 1; ++j) {
            vr[iny + j] = (vi[iny+ny + j] + vi[iny-ny + j] + vi[iny + j - 1] + vi[iny + j + 1]) *0.25;
        }
        vr[iny] = (vi[iny+ny] + vi[iny-ny] + 15.45 + vi[iny + 1])*0.25;
        vr[iny + ny - 1] = (vi[iny+ny + ny - 1] + vi[iny-ny + ny - 1] + vi[iny + ny - 2] - 6.7)*0.25;
        iny += ny;
    }
    for (j = 1; j < ny - 1; ++j) {
        vr[j] = (vi[ny + j] + 10. + vi[j - 1] + vi[j + 1]) *0.25;
        vr[(nx - 1)*ny + j] = (5. + vi[(nx - 2)*ny + j] + vi[(nx - 1)*ny + j - 1] + vi[(nx - 1)*ny + j + 1])*0.25;
    }


    //ÕâÀïÊÇÏòÎļþÖÐд±íµÄ²Ù×÷
    //vr¸³Öµ
    //ÕâÀï¶ÔviµÄÖµ½øÐÐÐÞ¸Ä

    iny = 0;
    for (i = 0; i<nx; ++i) {
        for (j = 0; j<ny - 2; ++j) {
            register double vr_(vr[iny + j]), vi_(vi[iny + j]);

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j <<" " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_=vr[iny + j]; vi_=vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
            ++j;

            vr_ = vr[iny + j]; vi_ = vi[iny + j];

            if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
                //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
                fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
            vi[iny + j] = (vr_ + vi_) * 0.5;
        }

        register double vr_(vr[iny + ny - 2]), vi_(vi[iny + ny - 2]);

        if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
            //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
            fout << "\n" << t << " " << i << " " << j << " " << vi_ << " " << vr_;
        vi[iny + ny - 2] = (vi_ + vr_) * 0.5;

        vr_ = fabs(vr[iny + ny - 1]); vi_=fabs(vi[iny + ny - 1]);

        if (vr_ - vi_<1e-2&&vr_ - vi_>-1e-2)
            //fprintf(fp, "\n%d %d %d %f %f", t, i, j, vi_, vr_);
            fout << "\n" << t << " " << i << " " << j <<" " << vi_ << " " << vr_;
        vi[iny + ny - 1] = (vi[iny + ny - 1] + vr[iny + ny - 1]) * 0.5;
        iny += ny;
    }

}

}

查看全部
weixin_43670526
weixin_43670526
2018/11/11 23:25
  • c++
  • gcc
  • 优化
  • 点赞
  • 收藏
  • 回答
    私信

1个回复