2015-05-01 63 views
-1

我正在努力嘗試一下。我讀過它可能會影響性能,但不如此代碼塊對我的機器有影響。Try-catch影響XNA的性能

foreach (Crew CurrentBall in CrewList) 
      { 
       if (CurrentBall != WhiteBall) 
       { 
        double distance = 0; 

        try 
        { 
         distance = Math.Sqrt((CurrentBall.Rectangle.X - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.X) 
          * (CurrentBall.Rectangle.X - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.X) 
          + (CurrentBall.Rectangle.Y - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.Y) 
          * (CurrentBall.Rectangle.Y - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.Y)); 
        } 
        catch 
        { 
         distance = Math.Sqrt((CurrentBall.Rectangle.X - WhiteBall.Rectangle.X) 
          * (CurrentBall.Rectangle.X - WhiteBall.Rectangle.X) 
          + (CurrentBall.Rectangle.Y - WhiteBall.Rectangle.Y) 
          * (CurrentBall.Rectangle.Y - WhiteBall.Rectangle.Y)); 
        } 

        if (distance < 75 && CurrentBall != WhiteBall) 
        { 
         if (CurrentBall.IsFollowingWhite == false) 
         { 
          NumberOfBallsFollowingWhite++; 
          CrewCatch.Play(); 
          Score += 5; 
          ReduceMovementSpeed(); 
          ApplyText(CurrentBall); 
          SnakeArray[NumberOfBallsFollowingWhite] = CurrentBall; 
          CurrentBall.SnakePosition = NumberOfBallsFollowingWhite; 
         } 

         CurrentBall.Follow(SnakeArray[CurrentBall.SnakePosition - 1]); 
         CurrentBall.IsFollowingWhite = true; 
        } 
       } 
      } 

它工作得很好,直到我用try-catch。我也嘗試沒有Math.Sqrt,但它沒有任何改善。

+1

try/catch塊中最昂貴的部分是實際捕獲(投擲,堆棧跟蹤等)。我認爲'if/else'會更適合你的情況。 – Stefan

+5

你應該清理輸入,不要依賴catch來執行不同的邏輯。 –

+0

你爲什麼使用try/catch?該機制永遠不會用於遊戲。 –

回答

3

這整個事情:

try 
{ 
    distance = Math.Sqrt((CurrentBall.Rectangle.X - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.X) 
     * (CurrentBall.Rectangle.X - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.X) 
     + (CurrentBall.Rectangle.Y - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.Y) 
     * (CurrentBall.Rectangle.Y - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.Y)); 
} 
catch 
{ 
    distance = Math.Sqrt((CurrentBall.Rectangle.X - WhiteBall.Rectangle.X) 
     * (CurrentBall.Rectangle.X - WhiteBall.Rectangle.X) 
     + (CurrentBall.Rectangle.Y - WhiteBall.Rectangle.Y) 
     * (CurrentBall.Rectangle.Y - WhiteBall.Rectangle.Y)); 
} 

可以降低到類似:

if (CurrentBall.SnakePosition - 1 >= 0 && CurrentBall.SnakePosition - 1 < SnakeArray.Length) 
{ 
    int xValue = CurrentBall.Rectangle.X - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.X; 
    int xSquared = xValue * xValue; 

    int yValue = CurrentBall.Rectangle.Y - SnakeArray[CurrentBall.SnakePosition - 1].Rectangle.Y; 
    int ySquared = yValue * yValue; 

    distance = Math.Sqrt(xSquared + ySquared); 
} 
else 
{ 
    //And similar for the above here... 
    distance = Math.Sqrt((CurrentBall.Rectangle.X - WhiteBall.Rectangle.X) 
          * (CurrentBall.Rectangle.X - WhiteBall.Rectangle.X) 
          + (CurrentBall.Rectangle.Y - WhiteBall.Rectangle.Y) 
          * (CurrentBall.Rectangle.Y - WhiteBall.Rectangle.Y)); 
} 

切勿使用try/catch語句來執行邏輯運算。如果你沒有對異常做些什麼,也絕不會Catch

也嘗試使您的邏輯易於閱讀。看看我在「if」的第一部分做了什麼?看看理解(和調試)要容易多少。並沒有性能損失,編譯器會優化它!

+0

_「如果你沒有對異常做一些事情,通常是正確的,但並非總是如此。有時候你想吞下它。例如[加載項管理器和部分信任](https://msdn.microsoft.com/en-us/magazine/cc163701.aspx) – MickyD

+0

即使如此,如果您正在捕獲它,您至少應該記錄或跟蹤它。它有一個例外的原因。說實話,我曾經吞下例外,但我總是感到骯髒:) –