SORU
15 NİSAN 2015, ÇARŞAMBA


8 kat daha hızlı std daha dizilerle Java::C vektör . Ben yanlış ne yaptım?

Asla onların boyutunu değiştirmek, birkaç büyük diziler ile aşağıdaki Java kodu var. Benim bilgisayarda 1100 ms çalışır.

C aynı kodu ben uygulanan ve std::vector kullanılır.

Aynı kod çalışır C uygulama zaman bilgisayarımda 8800 ms. Ben bu kadar yavaş çalışan yanlış ne yaptın?

Temelde kodu aşağıdakileri yapar:

for (int i = 0; i < numberOfCells;   i) {
        h[i] =  h[i]   1;
        floodedCells[i] =  !floodedCells[i];
        floodedCellsTimeInterval[i] =  !floodedCellsTimeInterval[i];
        qInflow[i] =  qInflow[i]   1;
}

20000 civarında bir boyut ile farklı diziler arasında dolaşır.

Aşağıdaki bağlantılar altında: her ikisi de uygulamaları bulabilirsiniz

(İdeone tek döngü 400 kez zaman sınırlaması nedeniyle 2000 kez yerine koşmaya. Ama burada bile üç kez) bir fark yoktur

CEVAP
15 NİSAN 2015, ÇARŞAMBA


Evet, c sürümünde önbellek bir çekiç alır. Bu JİT bu işlemek için daha donanımlı gibi görünüyor.

Eğer isUpdateNeeded for dış değiştirirseniz() daha kısa pasajlar. Fark uzaklaşır.

Aşağıdaki örnek, 4x bir hızlanma üretir.

void isUpdateNeeded() {
    for (int i = 0; i < numberOfCells;   i) {
        h[i] =  h[i]   1;
        floodedCells[i] =  !floodedCells[i];
        floodedCellsTimeInterval[i] =  !floodedCellsTimeInterval[i];
        qInflow[i] =  qInflow[i]   1;
        qStartTime[i] =  qStartTime[i]   1;
        qEndTime[i] =  qEndTime[i]   1;
    }

    for (int i = 0; i < numberOfCells;   i) {
        lowerFloorCells[i] =  lowerFloorCells[i]   1;
        cellLocationX[i] =  cellLocationX[i]   1;
        cellLocationY[i] =  cellLocationY[i]   1;
        cellLocationZ[i] =  cellLocationZ[i]   1;
        levelOfCell[i] =  levelOfCell[i]   1;
        valueOfCellIds[i] =  valueOfCellIds[i]   1;
        h0[i] =  h0[i]   1;
        vU[i] =  vU[i]   1;
        vV[i] =  vV[i]   1;
        vUh[i] =  vUh[i]   1;
        vVh[i] =  vVh[i]   1;
    }
    for (int i = 0; i < numberOfCells;   i) {
        vUh0[i] =  vUh0[i]   1;
        vVh0[i] =  vVh0[i]   1;
        ghh[i] =  ghh[i]   1;
        sfx[i] =  sfx[i]   1;
        sfy[i] =  sfy[i]   1;
        qIn[i] =  qIn[i]   1;
        for(int j = 0; j < nEdges;   j) {
            neighborIds[i * nEdges   j] = neighborIds[i * nEdges   j]   1;
        }
        for(int j = 0; j < nEdges;   j) {
            typeInterface[i * nEdges   j] = typeInterface[i * nEdges   j]   1;
        }
    }

}

Bu önbellek yavaşlama nedeni vardır özlüyor makul bir dereceye kadar gösterir. Ayrıca önemli değişkenler dişli bir çözüm kolayca oluşturulur bağımlı olduğunu unutmayın.

Düzen tekrar sağlandı

Stefans yorum başına bir yapı, orijinal boyutlarını kullanarak onları gruplandırma çalıştım. Bu, benzer bir şekilde acil önbellek basıncı kaldırır. Sonuç (CCFLAG-O3) c sürümü java sürümü  daha hızlı olduğunu.

İkaz ne kısa ne de çok.

#include <vector>
#include <cmath>
#include <iostream>



class FloodIsolation {
    struct item{
      char floodedCells;
      char floodedCellsTimeInterval;
      double valueOfCellIds;
      double h;
      double h0;
      double vU;
      double vV;
      double vUh;
      double vVh;
      double vUh0;
      double vVh0;
      double sfx;
      double sfy;
      double qInflow;
      double qStartTime;
      double qEndTime;
      double qIn;
      double nx;
      double ny;
      double ghh;
      double floorLevels;
      int lowerFloorCells;
      char flagInterface;
      char floorCompletelyFilled;
      double cellLocationX;
      double cellLocationY;
      double cellLocationZ;
      int levelOfCell;
    };
    struct inner_item{
      int typeInterface;
      int neighborIds;
    };

    std::vector<inner_item> inner_data;
    std::vector<item> data;

public:
    FloodIsolation() :
            numberOfCells(20000), inner_data(numberOfCells * nEdges), data(numberOfCells)
   {

    }
    ~FloodIsolation(){
    }

    void isUpdateNeeded() {
        for (int i = 0; i < numberOfCells;   i) {
            data[i].h = data[i].h   1;
            data[i].floodedCells = !data[i].floodedCells;
            data[i].floodedCellsTimeInterval = !data[i].floodedCellsTimeInterval;
            data[i].qInflow = data[i].qInflow   1;
            data[i].qStartTime = data[i].qStartTime   1;
            data[i].qEndTime = data[i].qEndTime   1;
            data[i].lowerFloorCells = data[i].lowerFloorCells   1;
            data[i].cellLocationX = data[i].cellLocationX   1;
            data[i].cellLocationY = data[i].cellLocationY   1;
            data[i].cellLocationZ = data[i].cellLocationZ   1;
            data[i].levelOfCell = data[i].levelOfCell   1;
            data[i].valueOfCellIds = data[i].valueOfCellIds   1;
            data[i].h0 = data[i].h0   1;
            data[i].vU = data[i].vU   1;
            data[i].vV = data[i].vV   1;
            data[i].vUh = data[i].vUh   1;
            data[i].vVh = data[i].vVh   1;
            data[i].vUh0 = data[i].vUh0   1;
            data[i].vVh0 = data[i].vVh0   1;
            data[i].ghh = data[i].ghh   1;
            data[i].sfx = data[i].sfx   1;
            data[i].sfy = data[i].sfy   1;
            data[i].qIn = data[i].qIn   1;
            for(int j = 0; j < nEdges;   j) {
                inner_data[i * nEdges   j].neighborIds = inner_data[i * nEdges   j].neighborIds   1;
                inner_data[i * nEdges   j].typeInterface = inner_data[i * nEdges   j].typeInterface   1;
            }
        }

    }

    static const int nEdges;
private:

    const int numberOfCells;

};

const int FloodIsolation::nEdges = 6;

int main() {
    FloodIsolation isolation;
    clock_t start = clock();
    for (int i = 0; i < 4400;   i) {
        if(i % 100 == 0) {
            std::cout << i << "\n";
        }
        isolation.isUpdateNeeded();
    }

    clock_t stop = clock();
    std::cout << "Time: " << difftime(stop, start) / 1000 << "\n";
}

Benim sonuç biraz daha özgün boyutları için Jerry Tabut farklıdır. Benim için farkları kalır. Peki java versiyonu, 1.7.0_75 olabilir.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Google Developers

    Google Devel

    23 AĞUSTOS 2007
  • Jonathan Leack

    Jonathan Lea

    26 ŞUBAT 2007
  • Need for Speed

    Need for Spe

    8 ŞUBAT 2006