Ho questo ciclo scritto in C++, che compilato con MSVC2010 richiede molto tempo per essere eseguito. (300ms)prestazioni strane in C++ (VC 2010)
for (int i=0; i<h; i++) {
for (int j=0; j<w; j++) {
if (buf[i*w+j] > 0) {
const int sy = max(0, i - hr);
const int ey = min(h, i + hr + 1);
const int sx = max(0, j - hr);
const int ex = min(w, j + hr + 1);
float val = 0;
for (int k=sy; k < ey; k++) {
for (int m=sx; m < ex; m++) {
val += original[k*w + m] * ds[k - i + hr][m - j + hr];
}
}
heat_map[i*w + j] = val;
}
}
}
Sembrava un po 'strano per me, così ho fatto alcuni test poi cambiato pochi bit di inline di montaggio: (in particolare, il codice che riassume "val")
for (int i=0; i<h; i++) {
for (int j=0; j<w; j++) {
if (buf[i*w+j] > 0) {
const int sy = max(0, i - hr);
const int ey = min(h, i + hr + 1);
const int sx = max(0, j - hr);
const int ex = min(w, j + hr + 1);
__asm {
fldz
}
for (int k=sy; k < ey; k++) {
for (int m=sx; m < ex; m++) {
float val = original[k*w + m] * ds[k - i + hr][m - j + hr];
__asm {
fld val
fadd
}
}
}
float val1;
__asm {
fstp val1
}
heat_map[i*w + j] = val1;
}
}
}
Ora funziona in metà tempo, 150ms. Fa esattamente la stessa cosa, ma perché è due volte più veloce? In entrambi i casi è stato eseguito in modalità Release con ottimizzazioni attive. Sto facendo qualcosa di sbagliato nel mio codice C++ originale?
Hai provato a confrontare il codice assembly generato in entrambi i casi ... –