2011-07-05 3 views
1

나는 이것이 아마도 당신이 할당하고있는 객체의 크기에 달려 있다는 것을 알고 있지만, 나는 내가 나중에 이런 문제들을 해결할 수있는 방법을 계속하고 있는지 알고 싶다. 지금 당장은 모든게 잘된 것 같습니다. 나는 Cocos2D를 사용하고있다. 내가 할당하고 for 루프의 모든 루프 checkDistance 할당을 해제하고있어 어떻게Objective C에서 할당 및 할당 해제는 얼마나 빠릅니까?

-(void) checkCGPointDistances{ 

    CCSprite *tempSp; 

    CCLabelTTF *tempLabel; 

    for(int i=0; i<hummingBirdsMax; ++i){ 

     tempSp = [hummingBirds objectAtIndex:i]; 

     checkDistance = [[CheckDistance alloc] init]; 

     if([checkDistance checkDistanceWithLimit:tempSp.position withPoint2:hero.heroSprite.position withLimit:150] == YES){ 

      if(hero.heroSprite.position.x > tempSp.position.x){ 
       tempSp.flipX = YES; 
      }else{ 
       tempSp.flipX = NO; 
      } 

      tempLabel = [hummingLabels.labelsArr objectAtIndex:i]; 


      [tempLabel setString:@"Hello!"]; 

      [hummingLabels.deactivateLabelToggle replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:1]]; 
     } 

     [checkDistance release]; 

    } 

    if(abilityRushH == 0){ 
     checkDistance = [[CheckDistance alloc] init]; 

     if([checkDistance checkDistanceWithLimit:rushH.rushHSprite.position withPoint2:hero.heroSprite.position withLimit:32] == YES){ 
      [rushH.rushHSprite removeFromParentAndCleanup:NO]; 

      abilityRushH = 1; 
      NSLog(@"horizontal rush activated"); 
     } 

     [checkDistance release]; 
    } 

    if(abilityRushV == 0){ 
     checkDistance = [[CheckDistance alloc] init]; 

     if([checkDistance checkDistanceWithLimit:rushV.rushVSprite.position withPoint2:hero.heroSprite.position withLimit:32] == YES){ 
      [rushV.rushVSprite removeFromParentAndCleanup:NO]; 

      abilityRushV = 1; 
      NSLog(@"vertical rush activated"); 
     } 

     [checkDistance release]; 
    } 
} 

주의 사항 :

는 예를 들어이 방법을 가지고? 이 메서드는 매우 빠르게 타이머에서 호출됩니다. 루프 바깥쪽에 반복적으로 할당을하기로 결정한 이유는 hummingBirdsMax의 수가 0 일 수 있기 때문입니다. 따라서 할당을 전혀 할 필요가 없습니다. 나는 단지 hummingBirdsMax> 0인지를 확인할 수있을 것이라고 생각한다.하지만 내가하는 일이 그다지 다르지 않은가?

할당하는 클래스는 CGPoints 사이의 거리를 확인하고 bool을 반환합니다.

+0

'CheckDistance'구현을 게시 할 수 있습니까? 특히'init' 메소드는 무엇입니까? – Yuji

답변

3

본인이하는 일에 근본적으로 잘못된 것은 없습니다. 객체 할당 및 할당 해제가 프로파일 러에서 어떤 작업 (상어 또는 계측기)없이 코드의 성능 병목 현상인지 여부를 알 수있는 방법이 없습니다. 모든 성능 최적화 결정과 마찬가지로 일부 데이터가 없으면 추측 할 이유가 없습니다.

즉, 모든 CheckDistance 클래스가 계산을 수행하면 실제로는 객체가 아니며 상태를 캡슐화하고 동작을 노출합니다. 왜 C 함수로 계산을 구현하지 않는가? 많은 사람들이 지금 익숙해 져있는 관리되는 Java/C# 세계와 달리 모든 것이 클래스가 아니어야합니다.

+0

감사합니다 배리. 클래스 외부에있는 일반적인 C 함수 인 경우 별도의 클래스에 있어야하지 않습니까? 다른 클래스 안에 넣는 메소드 중 일부는 그렇게하기에 충분할만큼 커야합니다. 그렇지 않으면 한 클래스에 너무 많은 코드가 생깁니다. – VagueExplanation

+0

@VagueExplanation, 코드가 * 어떤 * 클래스에있을 필요는 없습니다. 다른 코드에서 호출 할 수 있도록 헤더 파일에 함수를 선언해야하지만,'.m' 파일에 정의를 넣을 수 있습니다. 사실 CoreFoundation (및 다른 많은 OS X/iOS) API는 C 함수입니다 ... –

+0

감사합니다 배리 이것은 훌륭하지만 정의가 다른 .m 파일에 있으면 어떻게 호출할까요? 지금 저는 hero.vel = [velocity getVel : hero.heroSprite.position withEndPoint : heroControl.moveEndLoc withSpeed ​​: hero.speed]와 같은 것을합니다. – VagueExplanation