- Home /
Try and catch very slow
ok so i have mapNodes[x,z] on a grid the GameObject node[x,z] stores the MapNode[x,z] script . This works however it is extremly slow, why? public void findAllAdjacentNodes(){
for (int x = 0 ; x < mapWidth ; x++)
{
for (int z = 0 ; z< mapHeight ; z++)
{
try{
mapNodes[x,z].northNode = mapNodes[x,z+1]; } catch{}
try{
mapNodes[x,z].southNode = mapNodes[x,z-1]; } catch{}
try{
mapNodes[x,z].eastNode = mapNodes[x+1,z]; } catch{}
try{
mapNodes[x,z].westNode = mapNodes[x-1,z]; } catch{}
}
}
}
Read this page : http://answers.unity3d.com/page/newuser.html
For any help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
Answer by Yokimato · Apr 26, 2013 at 10:10 PM
Try and catch statements are not slow. Period.
Exception handling is done in the CLR and is one of the faster operations in c#.
With that said you have way too many of them. I suggest starting with an introduction to programming since its clear you're not familiar with using them. Once you understand the concept more, solving this issue will be a breeze and you'll be better off for it.
Good luck.
thanks for the speedy reply,just read up on it. try catch exceptionhandling has almost no overhead until the try statement triggers an exception. exception handling then becomes costly, in my case I'm handling a ton of index OutOfBounds exceptions. is there a better way to modify this?
i guess ill just scrap the exception handiling and use an if statement avoiding both the null values and the bounds with .length . thanks!