μΉ΄ν…Œκ³ λ¦¬ 보관물: C++

C++

JavaλŠ” C ++의 std :: vector보닀 배열을 μ‚¬μš©ν•˜μ—¬ 8 λ°° 더 λΉ λ¦…λ‹ˆλ‹€. λ‚΄κ°€ 뭘 잘λͺ» ν–ˆμ–΄? μˆ˜ν–‰ν•©λ‹ˆλ‹€. for (int i

크기λ₯Ό λ³€κ²½ν•˜μ§€ μ•ŠλŠ” λͺ‡ 개의 큰 λ°°μ—΄μ΄μžˆλŠ” λ‹€μŒ Java μ½”λ“œκ°€ μžˆμŠ΅λ‹ˆλ‹€. λ‚΄ μ»΄ν“¨ν„°μ—μ„œ 1100ms에 μ‹€ν–‰λ©λ‹ˆλ‹€.

C ++μ—μ„œ λ™μΌν•œ μ½”λ“œλ₯Ό κ΅¬ν˜„ν•˜κ³  std::vector.

λ˜‘κ°™μ€ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λŠ” C ++ κ΅¬ν˜„ μ‹œκ°„μ€ λ‚΄ μ»΄ν“¨ν„°μ—μ„œ 8800msμž…λ‹ˆλ‹€. λ‚΄κ°€ 뭘 잘λͺ» ν–ˆλ‚˜μš”? μ΄λ ‡κ²Œ 느리게 μ‹€ν–‰ λ˜λ‚˜μš”?

기본적으둜 μ½”λ“œλŠ” λ‹€μŒμ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

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 크기의 λ‹€λ₯Έ 배열을 λ°˜λ³΅ν•©λ‹ˆλ‹€.

λ‹€μŒ λ§ν¬μ—μ„œ 두 κ°€μ§€ κ΅¬ν˜„μ„ λͺ¨λ‘ 찾을 수 μžˆμŠ΅λ‹ˆλ‹€.

(ideoneμ—μ„œλŠ” μ‹œκ°„ μ œν•œμœΌλ‘œ 인해 2000 번이 μ•„λ‹Œ 400 번만 루프λ₯Ό μ‹€ν–‰ν•  수 μžˆμ—ˆμ§€λ§Œ μ—¬κΈ°μ—μ„œλ„ μ„Έ 번의 차이가 μžˆμŠ΅λ‹ˆλ‹€)



λ‹΅λ³€

λ‹€μŒμ€ λ…Έλ“œ 별 데이터가 ꡬ쑰둜 μˆ˜μ§‘λ˜κ³  ν•΄λ‹Ή ꡬ쑰의 단일 벑터가 μ‚¬μš© 된 C ++ λ²„μ „μž…λ‹ˆλ‹€.

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



class FloodIsolation {
public:
  FloodIsolation() :
      numberOfCells(20000),
      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) {
        data[i].flagInterface[j] = !data[i].flagInterface[j];
        data[i].typeInterface[j] = data[i].typeInterface[j] + 1;
        data[i].neighborIds[j] = data[i].neighborIds[j] + 1;
      }
    }

  }

private:

  const int numberOfCells;
  static const int nEdges = 6;
  struct data_t {
    bool floodedCells = 0;
    bool floodedCellsTimeInterval = 0;

    double valueOfCellIds = 0;
    double h = 0;

    double h0 = 0;
    double vU = 0;
    double vV = 0;
    double vUh = 0;
    double vVh = 0;
    double vUh0 = 0;
    double vVh0 = 0;
    double ghh = 0;
    double sfx = 0;
    double sfy = 0;
    double qInflow = 0;
    double qStartTime = 0;
    double qEndTime = 0;
    double qIn = 0;
    double nx = 0;
    double ny = 0;
    double floorLevels = 0;
    int lowerFloorCells = 0;
    bool floorCompleteleyFilled = 0;
    double cellLocationX = 0;
    double cellLocationY = 0;
    double cellLocationZ = 0;
    int levelOfCell = 0;
    bool flagInterface[nEdges] = {};
    int typeInterface[nEdges] = {};
    int neighborIds[nEdges] = {};
  };
  std::vector<data_t> data;

};

int main() {
  std::ios_base::sync_with_stdio(false);
  FloodIsolation isolation;
  clock_t start = clock();
  for (int i = 0; i < 400; ++i) {
    if(i % 100 == 0) {
      std::cout << i << "\n";
    }
    isolation.isUpdateNeeded();
  }
  clock_t stop = clock();
  std::cout << "Time: " << difftime(stop, start) / 1000 << "\n";
}

라이브 예

μ‹œκ°„μ€ 이제 Java λ²„μ „μ˜ 2 λ°°μž…λ‹ˆλ‹€. (846 λŒ€ 1631).

ν™•λ₯ μ€ JITκ°€ λͺ¨λ“  κ³³μ—μ„œ 데이터에 μ•‘μ„ΈμŠ€ν•˜λŠ” μΊμ‹œ κ΅½κΈ°λ₯Ό λ°œκ²¬ν•˜κ³  μ½”λ“œλ₯Ό λ…Όλ¦¬μ μœΌλ‘œ μœ μ‚¬ν•˜μ§€λ§Œ 더 효율적인 μˆœμ„œλ‘œ λ³€ν™˜ν–ˆμŠ΅λ‹ˆλ‹€.

당신이 ν˜Όν•©ν•˜λŠ” 경우 κ·Έμ—λ§Œ ν•„μš”λ‘œ λ˜ν•œ, ν‘œμ€€ μž…μΆœλ ₯ 동기화λ₯Ό 끌 printf/ scanfC ++둜 std::cout와 std::cin. λ°œμƒν•˜λŠ”λŒ€λ‘œ λͺ‡ κ°€μ§€ κ°’λ§Œ μΈμ‡„ν•˜μ§€λ§Œ C ++의 κΈ°λ³Έ 인쇄 λ™μž‘μ€ μ§€λ‚˜μΉ˜κ²Œ νŽΈμ§‘μ¦μ μ΄κ³  λΉ„νš¨μœ¨μ μž…λ‹ˆλ‹€.

이 nEdgesμ‹€μ œ μƒμˆ˜ 값이 μ•„λ‹Œ 경우 3 개의 β€œλ°°μ—΄β€κ°’μ„ struct. 그것은 큰 μ„±λŠ₯ μ €ν•˜λ₯Ό μΌμœΌν‚€μ§€ μ•Šμ•„μ•Όν•©λ‹ˆλ‹€.

struct크기λ₯Ό μ€„μž„μœΌλ‘œμ¨ 값을 μ •λ ¬ν•˜μ—¬ λ©”λͺ¨λ¦¬ ν’‹ ν”„λ¦°νŠΈλ₯Ό μ€„μž„μœΌλ‘œμ¨ (그리고 μ€‘μš”ν•˜μ§€ μ•Šμ€ 경우 μ•‘μ„ΈμŠ€λ„ μ •λ ¬ν•˜μ—¬) 또 λ‹€λ₯Έ μ„±λŠ₯ ν–₯상을 얻을 수 μžˆμŠ΅λ‹ˆλ‹€ . ν•˜μ§€λ§Œ ν™•μ‹€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

κ²½ν—˜μƒ 단일 μΊμ‹œ λ―ΈμŠ€κ°€ λͺ…령어보닀 100 λ°° 더 λΉ„μŒ‰λ‹ˆλ‹€. μΊμ‹œ 일관성을 갖도둝 데이터λ₯Ό λ°°μ—΄ν•˜λŠ” 것은 λ§Žμ€ κ°€μΉ˜κ°€ μžˆμŠ΅λ‹ˆλ‹€.

데이터λ₯Ό a둜 재배 μ—΄ν•˜λŠ” struct것이 λΆˆκ°€λŠ₯ν•œ 경우 각 μ»¨ν…Œμ΄λ„ˆμ— λŒ€ν•΄ μ°¨λ‘€λ‘œ λ°˜λ³΅ν•˜λ„λ‘ λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ œμ³λ‘κ³  Java 및 C ++ λ²„μ „μ—λŠ” μ•½κ°„μ˜ λ―Έλ¬˜ν•œ 차이가 μžˆμŠ΅λ‹ˆλ‹€. λ‚΄κ°€ 발견 ν•œ 것은 Java 버전은 β€œfor each edge”루프에 3 개의 λ³€μˆ˜κ°€μžˆλŠ” 반면 C ++ λ²„μ „μ—λŠ” 2 개만 μžˆμ—ˆλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. μ €λŠ” Java와 μΌμΉ˜ν•˜λ„λ‘ λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€. λ‹€λ₯Έ μ‚¬λžŒμ΄ μžˆλŠ”μ§€ λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.


λ‹΅λ³€

예, C ++ λ²„μ „μ˜ μΊμ‹œλŠ” 망치질이 ν•„μš”ν•©λ‹ˆλ‹€. JIT가이λ₯Ό μ²˜λ¦¬ν•˜κΈ° μœ„ν•΄ 더 잘 κ°–μΆ”μ–΄ μ§„ 것 κ°™μŠ΅λ‹ˆλ‹€.

forisUpdateNeeded () 의 μ™ΈλΆ€ λ₯Ό 더 짧은 슀 λ‹ˆνŽ«μœΌλ‘œ λ³€κ²½ν•˜λŠ” 경우 . κ·Έ μ°¨μ΄λŠ” μ‚¬λΌμ§‘λ‹ˆλ‹€.

μ•„λž˜ μƒ˜ν”Œμ€ 4 배의 속도 ν–₯상을 μ œκ³΅ν•©λ‹ˆλ‹€.

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;
        }
    }

}

이것은 μΊμ‹œ λ―ΈμŠ€κ°€ 속도 μ €ν•˜μ˜ μ›μΈμ΄λΌλŠ” 것을 ν•©λ¦¬μ μœΌλ‘œ λ³΄μ—¬μ€λ‹ˆλ‹€. λ˜ν•œ λ³€μˆ˜κ°€ 쒅속적이지 μ•ŠμœΌλ―€λ‘œ μŠ€λ ˆλ“œ μ†”λ£¨μ…˜μ„ μ‰½κ²Œ λ§Œλ“€ 수 μžˆλ‹€λŠ” 점에 μœ μ˜ν•΄μ•Όν•©λ‹ˆλ‹€.

주문이 λ³΅μ›λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

stefans μ˜κ²¬μ— 따라 μ›λž˜ 크기λ₯Ό μ‚¬μš©ν•˜μ—¬ ꡬ쑰체둜 κ·Έλ£Ήν™”ν•˜λ €κ³ ν–ˆμŠ΅λ‹ˆλ‹€. 이것은 μœ μ‚¬ν•œ λ°©μ‹μœΌλ‘œ 즉각적인 μΊμ‹œ μ••λ ₯을 μ œκ±°ν•©λ‹ˆλ‹€. κ·Έ κ²°κ³Ό c ++ (CCFLAG -O3) 버전이 μžλ°” 버전보닀 μ•½ 15 % λΉ λ¦…λ‹ˆλ‹€.

짧지도 μ˜ˆμ˜μ§€λ„ μ•Šμ€ Varning.

#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";
}

λ‚΄ κ²°κ³ΌλŠ” μ›λž˜ 크기의 Jerry Coffins와 μ•½κ°„ λ‹€λ¦…λ‹ˆλ‹€. λ‚˜μ—κ²ŒλŠ” 차이점이 남아 μžˆμŠ΅λ‹ˆλ‹€. λ‚΄ μžλ°” 버전 1.7.0_75 일 수 μžˆμŠ΅λ‹ˆλ‹€.


λ‹΅λ³€

@Stefan이 @CaptainGiraffe의 닡변에 λŒ€ν•œ μ˜κ²¬μ—μ„œ μΆ”μΈ‘ν–ˆλ“―μ΄ 벑터 ꡬ쑰체 λŒ€μ‹  ꡬ쑰체 벑터λ₯Ό μ‚¬μš©ν•˜μ—¬ μƒλ‹Ήν•œ 이득을 μ–»μŠ΅λ‹ˆλ‹€. μˆ˜μ • 된 μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

#include <vector>
#include <cmath>
#include <iostream>
#include <time.h>

class FloodIsolation {
public:
    FloodIsolation() :
            h(0),
            floodedCells(0),
            floodedCellsTimeInterval(0),
            qInflow(0),
            qStartTime(0),
            qEndTime(0),
            lowerFloorCells(0),
            cellLocationX(0),
            cellLocationY(0),
            cellLocationZ(0),
            levelOfCell(0),
            valueOfCellIds(0),
            h0(0),
            vU(0),
            vV(0),
            vUh(0),
            vVh(0),
            vUh0(0),
            vVh0(0),
            ghh(0),
            sfx(0),
            sfy(0),
            qIn(0),
            typeInterface(nEdges, 0),
            neighborIds(nEdges, 0)
    {
    }

    ~FloodIsolation(){
    }

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

private:

    static const int nEdges = 6;
    bool floodedCells;
    bool floodedCellsTimeInterval;

    std::vector<int> neighborIds;
    double valueOfCellIds;
    double h;
    double h0;
    double vU;
    double vV;
    double vUh;
    double vVh;
    double vUh0;
    double vVh0;
    double ghh;
    double sfx;
    double sfy;
    double qInflow;
    double qStartTime;
    double qEndTime;
    double qIn;
    double nx;
    double ny;
    double floorLevels;
    int lowerFloorCells;
    bool flagInterface;
    std::vector<int> typeInterface;
    bool floorCompleteleyFilled;
    double cellLocationX;
    double cellLocationY;
    double cellLocationZ;
    int levelOfCell;
};

int main() {
    std::vector<FloodIsolation> isolation(20000);
    clock_t start = clock();
    for (int i = 0; i < 400; ++i) {
        if(i % 100 == 0) {
            std::cout << i << "\n";
        }

        for (auto &f : isolation)
            f.Update();
    }
    clock_t stop = clock();
    std::cout << "Time: " << difftime(stop, start) / 1000 << "\n";
}

VC ++ 2015 CTPμ—μ„œ 컴파일러둜 μ»΄νŒŒμΌν•˜μ—¬λ₯Ό μ‚¬μš©ν•˜μ—¬ -EHsc -O2b2 -GL -Qparλ‹€μŒκ³Ό 같은 κ²°κ³Όλ₯Ό μ–»μŠ΅λ‹ˆλ‹€.

0
100
200
300
Time: 0.135

g ++둜 μ»΄νŒŒμΌν•˜λ©΄ μ•½κ°„ 느린 κ²°κ³Όκ°€ μƒμ„±λ©λ‹ˆλ‹€.

0
100
200
300
Time: 0.156

λ™μΌν•œ ν•˜λ“œμ›¨μ–΄μ—μ„œ Java 8u45의 컴파일러 / JVM을 μ‚¬μš©ν•˜λ©΄ λ‹€μŒκ³Ό 같은 κ²°κ³Όκ°€ λ‚˜νƒ€λ‚©λ‹ˆλ‹€.

0
100
200
300
Time: 181

μ΄λŠ” VC ++ 버전보닀 μ•½ 35 % 느리고 g ++ 버전보닀 μ•½ 16 % λŠλ¦½λ‹ˆλ‹€.

반볡 횟수λ₯Ό μ›ν•˜λŠ” 2000으둜 늘리면 차이가 3 %둜 λ–¨μ–΄μ§‘λ‹ˆλ‹€.이 경우 C ++의 μž₯점 쀑 μΌλΆ€λŠ” μ‹€μ œλ‘œ μ‹€ν–‰ μžμ²΄κ°€ μ•„λ‹ˆλΌ λ‹¨μˆœνžˆ 더 λΉ λ₯Έ λ‘œλ”© (Java의 지속적인 문제)μ΄λΌλŠ” 것을 μ•”μ‹œν•©λ‹ˆλ‹€. 이 κ²½μš°μ— 이것은 λ†€λΌμš΄ 일이 μ•„λ‹™λ‹ˆλ‹€. (κ²Œμ‹œ 된 μ½”λ“œμ—μ„œ) μΈ‘μ •λ˜λŠ” 계산이 λ„ˆλ¬΄ μ‚¬μ†Œν•΄μ„œ λŒ€λΆ€λΆ„μ˜ μ»΄νŒŒμΌλŸ¬κ°€μ΄λ₯Ό μ΅œμ ν™”ν•˜κΈ° μœ„ν•΄ λ§Žμ€ 일을 ν•  수 μžˆμ„μ§€ μ˜μ‹¬ μŠ€λŸ½μŠ΅λ‹ˆλ‹€.


λ‹΅λ³€

λ‚˜λŠ” 이것이 λ©”λͺ¨λ¦¬ 할당에 κ΄€ν•œ 것이라고 μƒκ°ν•©λ‹ˆλ‹€.

λ‚˜λŠ” Javaν”„λ‘œκ·Έλž¨ μ‹œμž‘μ‹œ 큰 연속 블둝 을 작고 C++OS에 λΉ„νŠΈμ™€ 쑰각을 μš”μ²­ν•˜λŠ” κ²ƒμœΌλ‘œ μƒκ°ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

이 이둠을 ν…ŒμŠ€νŠΈν•˜κΈ° μœ„ν•΄ C++버전 을 ν•œ 번 μˆ˜μ •ν–ˆλŠ”λ° κ°‘μžκΈ° Java버전 보닀 μ•½κ°„ λΉ λ₯΄κ²Œ μ‹€ν–‰λ˜κΈ° μ‹œμž‘ν–ˆμŠ΅λ‹ˆλ‹€ .

int main() {
    {
        // grab a large chunk of contiguous memory and liberate it
        std::vector<double> alloc(20000 * 20);
    }
    FloodIsolation isolation;
    clock_t start = clock();
    for (int i = 0; i < 400; ++i) {
        if(i % 100 == 0) {
            std::cout << i << "\n";
        }
        isolation.isUpdateNeeded();
    }
    clock_t stop = clock();
    std::cout << "Time: " << (1000 * difftime(stop, start) / CLOCKS_PER_SEC) << "\n";
}

사전 ν• λ‹Ή 벑터가 μ—†λŠ” λŸ°νƒ€μž„ :

0
100
200
300
Time: 1250.31

사전 ν• λ‹Ή 벑터λ₯Ό μ‚¬μš©ν•œ λŸ°νƒ€μž„ :

0
100
200
300
Time: 331.214

Java버전 용 λŸ°νƒ€μž„ :

0
100
200
300
Time: 407

λ‹΅λ³€