ν¬κΈ°λ₯Ό λ³κ²½νμ§ μλ λͺ κ°μ ν° λ°°μ΄μ΄μλ λ€μ 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 ν¬κΈ°μ λ€λ₯Έ λ°°μ΄μ λ°λ³΅ν©λλ€.
λ€μ λ§ν¬μμ λ κ°μ§ ꡬνμ λͺ¨λ μ°Ύμ μ μμ΅λλ€.
- μλ° : https://ideone.com/R8KqjT
- C ++ : https://ideone.com/Lu7RpE
(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
/ scanf
C ++λ‘ std::cout
μ std::cin
. λ°μνλλλ‘ λͺ κ°μ§ κ°λ§ μΈμνμ§λ§ C ++μ κΈ°λ³Έ μΈμ λμμ μ§λμΉκ² νΈμ§μ¦μ μ΄κ³ λΉν¨μ¨μ μ
λλ€.
μ΄ nEdges
μ€μ μμ κ°μ΄ μλ κ²½μ° 3 κ°μ βλ°°μ΄βκ°μ struct
. κ·Έκ²μ ν° μ±λ₯ μ νλ₯Ό μΌμΌν€μ§ μμμΌν©λλ€.
struct
ν¬κΈ°λ₯Ό μ€μμΌλ‘μ¨ κ°μ μ λ ¬νμ¬ λ©λͺ¨λ¦¬ ν νλ¦°νΈλ₯Ό μ€μμΌλ‘μ¨ (κ·Έλ¦¬κ³ μ€μνμ§ μμ κ²½μ° μ‘μΈμ€λ μ λ ¬νμ¬) λ λ€λ₯Έ μ±λ₯ ν₯μμ μ»μ μ μμ΅λλ€ . νμ§λ§ νμ€νμ§ μμ΅λλ€.
κ²½νμ λ¨μΌ μΊμ λ―Έμ€κ° λͺ λ Ήμ΄λ³΄λ€ 100 λ°° λ λΉμλλ€. μΊμ μΌκ΄μ±μ κ°λλ‘ λ°μ΄ν°λ₯Ό λ°°μ΄νλ κ²μ λ§μ κ°μΉκ° μμ΅λλ€.
λ°μ΄ν°λ₯Ό aλ‘ μ¬λ°° μ΄νλ struct
κ²μ΄ λΆκ°λ₯ν κ²½μ° κ° μ»¨ν
μ΄λμ λν΄ μ°¨λ‘λ‘ λ°λ³΅νλλ‘ λ³κ²½ν μ μμ΅λλ€.
μ μ³λκ³ Java λ° C ++ λ²μ μλ μ½κ°μ λ―Έλ¬ν μ°¨μ΄κ° μμ΅λλ€. λ΄κ° λ°κ²¬ ν κ²μ Java λ²μ μ βfor each edgeβ루νμ 3 κ°μ λ³μκ°μλ λ°λ©΄ C ++ λ²μ μλ 2 κ°λ§ μμλ€λ κ²μ λλ€. μ λ Javaμ μΌμΉνλλ‘ λ§λ€μμ΅λλ€. λ€λ₯Έ μ¬λμ΄ μλμ§ λͺ¨λ₯΄κ² μ΅λλ€.
λ΅λ³
μ, C ++ λ²μ μ μΊμλ λ§μΉμ§μ΄ νμν©λλ€. JITκ°μ΄λ₯Ό μ²λ¦¬νκΈ° μν΄ λ μ κ°μΆμ΄ μ§ κ² κ°μ΅λλ€.
for
isUpdateNeeded () μ μΈλΆ λ₯Ό λ μ§§μ μ€ λν«μΌλ‘ λ³κ²½νλ κ²½μ° . κ·Έ μ°¨μ΄λ μ¬λΌμ§λλ€.
μλ μνμ 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