Wie @Stefan in einem Kommentar auf @ CaptainGiraffes Antwort erraten hat, gewinnen Sie ziemlich viel, indem Sie einen Vektor von Strukturen anstelle einer Struktur von Vektoren verwenden.Korrigierte Code wie folgt aussieht:
#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";
}
Zusammengestellt mit dem Compiler von VC++ 2015 CTP, mit -EHsc -O2b2 -GL -Qpar
, erhalte ich Ergebnisse wie:
0
100
200
300
Time: 0.135
mit g Kompilieren ++ ein Ergebnis erzeugt, die etwas langsamer ist:
0
100
200
300
Time: 0.156
Auf der gleichen Hardware, mit dem Compiler/JVM von Java 8u45, bekomme ich Ergebnisse wie:
0
100
200
300
Time: 181
Dies ist etwa 35% langsamer als die Version von VC++ und etwa 16% langsamer als die Version von g ++.
Wenn wir die Anzahl der Iterationen auf die gewünschten 2000 erhöhen, sinkt der Unterschied auf nur 3%, was darauf hindeutet, dass ein Teil des Vorteils von C++ in diesem Fall einfach schneller Laden (ein mehrjähriges Problem mit Java) ist Ausführung selbst. Das überrascht mich in diesem Fall nicht - die Berechnung, die gemessen wird (im geposteten Code) ist so trivial, dass ich bezweifle, dass die meisten Compiler eine Menge tun können, um sie zu optimieren. Hier
Was sind diese nicht gleich Operationen innerhalb der Schleife? – Steephen
Wie diese Vektoren definiert sind? –
Haben Sie den C++ Code im Debug-Modus oder im Release-Modus ausgeführt? – fredoverflow