我是第一次使用Visual Studio剖析器,我正試圖解釋結果。看著左邊的百分比,我發現這個減法的時間成本有點奇怪:
代碼的其他部分包含更復雜的運算式,例如:
即使是簡單的乘法似乎也比減法快得多 :
其他的乘法運算需要更長的時間,我真的不明白為什么,就像這樣 :
因此,我想我的問題是這里是否有什么奇怪的事情發生。
復雜的運算式所花費的時間比那個減法要長,而且一些運算式所花費的時間遠遠超過其他類似的運算式。我運行了幾次分析器,百分比的分布總是這樣的。我是不是解釋錯了?
更新:
有人要求我提供整個函式的剖析,所以它就在這里,盡管它有點大。我在for回圈中運行了這個函式1分鐘,得到了5萬個樣本。該函式包含一個雙回圈。為了方便起見,我先附上文字,然后是剖析的圖片。請注意,文本中的代碼有一點更新。
for (int i = 0; i < NUMBER_OF_CONTOUR_POINTS; i ) {
vec4 contourPointV(contour3DPoints[i], 1)。
float phi = angles[i];
float xW = pose[0][0] * contourPointV.x pose[1] [0] * contourPointV. y contourPointV.z * pose[2][0] pose[3] [0]。
float yW = pose[0][1] * contourPointV.x pose[1] [1] * contourPointV. y contourPointV.z * pose[2][1] pose[3][1] 。
float zW = pose[0][2] * contourPointV.x pose[1] [2] * contourPointV. y contourPointV.z * pose[2][2] pose[3][2] 。
float x = -G_FU_STRICT * xW / zW;
float y = -G_FV_STRICT * yW / zW;
x = (x 1) * G_WIDTHo2;
y = (y 1) * G_HEIGHTo2;
y = G_HEIGHT - y;
phi -= extraTheta;
if (phi < 0)phi = CV_PI2;
int indexForTable = phi * oneKoverPI;
//vec2 ray(cos(phi), sin(phi));
vec2 ray(cos_pre[indexForTable], sin_pre[indexForTable])。
vec2 ray2(-ray.x, -ray.y)。
float outerStepX = ray.x * step;
float outerStepY = ray.y * step;
cv::Point2f outerPoint(x outerStepX, y outerStepY)。
cv::Point2f innerPoint(x - outerStepX, y - outerStepY)。
cv::Point2f contourPointCV(x, y)。
cv::Point2f contourPointCVcopy(x, y)。
bool cut = false。
if (!isInView(outerPoint.x, outerPoint.y) || !isInView(innerPoint.x, innerPoint.y) ) {
cut = true。
}
bool outside2 = true; bool outside1 = true;
if (cut) {
outside2 = myClipLine(contourPointCV.x, contourPointCV.y, outerPoint.x, outerPoint.y, G_WIDTH - 1, G_HEIGHT - 1) 。
outside1 = myClipLine(contourPointCVcopy.x, contourPointCVcopy.y, innerPoint.x, innerPoint.y, G_WIDTH - 1, G_HEIGHT - 1)。
}
myIterator innerRayMine(contourPointCVcopy, innerPoint)。
myIterator outerRayMine(contourPointCV, outerPoint)。
if (! outside1) {
innerRayMine.end = true;
innerRayMine.prob = true;
}
if (!outside2) {
outerRayMine.end = true;
innerRayMine.prob = true;
}
vec2 normal = -ray;
float dfdxTerm = -normal.x;
float dfdyTerm = normal.y。
vec3 point3D = vec3(xW, yW, zW)。
cv::Point contourPoint((int)x, (int)y);
float Xc = point3D.x; float Xc2 = Xc * Xc; float Yc = point3D. y; float Yc2 = Yc * Yc; float Zc = point3D.z; float Zc2 = Zc * Zc。
float XcYc = Xc * Yc; float dfdxFu = dfdxTerm * G_FU; float dfdyFv = dfdyTerm * G_FU。float overZc2 = 1 / Zc2; float overZc = 1 / Zc。
pixelJacobi[0] = (dfdyFv * (Yc2 Zc2) dfdxFu * XcYc) * overZc2;
pixelJacobi[1] = (-dfdxFu * (Xc2 Zc2) - dfdyFv * XcYc) * overZc2;
pixelJacobi[2] = (-dfdyFv * Xc dfdxFu * Yc) * overZc;
pixelJacobi[3] = -dfdxFu * overZc;
pixelJacobi[4] = -dfdyFv * overZc;
pixelJacobi[5] = (dfdyFv * Yc dfdxFu * Xc) * overZc2;
float commonFirstTermsSum = 0;
float commonFirstTermsSquaredSum = 0;
int test = 0;
while (!innerRayMine.end) {
test ;
cv::Point xy = innerRayMine.pos(); innerRayMine ;
int x = xy.x。
int y = xy.y;
float dx = x - contourPoint.x;
float dy = y - contourPoint.y;
vec2 dxdy(dx, dy)。
float raw = -glm::dot(dxdy, normal)。
float heavisideTerm = heaviside_pre[(int)raw * 100 1000] 。
float deltaTerm = delta_pre[(int)raw * 100 1000]。
const Vec3b rgb = ante[y * 640 x] 。
int red = rgb[0]; int green = rgb[1] 。int blue = rgb[2] 。
red = red >> 3; red = red << 10; green = green > > 3; green = green << 5; blue = blue >> 3;
int colorIndex = red green blue;
pF = pFPointer[colorIndex];
pB = pBPointer[colorIndex];
float denAsMul = 1 / (pF pB 0.000001) 。
pF = pF * denAsMul;
float pfMinusPb = 2 * pF - 1;
float denominator = heavisideTerm * (pfMinusPb) pB 0.000001;
float commonFirstTerm = -pfMinusPb / denominator * deltaTerm;
commonFirstTermsSum = commonFirstTerm;
commonFirstTermsSquaredSum = commonFirstTerm * commonFirstTerm;
}
<
uj5u.com熱心網友回復:
Visual Studio通過取樣進行剖析:它經常中斷執行,并記錄指令指標的值;然后將其映射到原始碼,并計算出擊中該行的頻率。
這樣做有一些問題:在優化的代碼中,并不總是能夠弄清楚哪一行產生了特定的匯編指令。
我使用的一個技巧是將感興趣的代碼移到一個單獨的函式中,并用__declspec(noinline) 來宣告它。
在你的例子中,你確定減法的執行次數與乘法一樣多嗎?我更困惑的是隨后的乘法的差異(0.39%和0.53%)
更新:
我認為以下幾行:
float phi = angles[i];
并且
phi -= extraTheta;
在匯編中被移到一起,獲得angles[i]的時間被添加到該減法行中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/332624.html
標籤:
上一篇:PowerShell的計時回圈沒有完成,在隨機位置掛起
下一篇:如何找到一個網站的正確API?











