开发者

How fast is allocation and deallocation in Objective C?

开发者 https://www.devze.com 2023-03-17 23:58 出处:网络
I know this probably depends on how large of an object you are allocating, but I just want to know if I keep on doing things this way I will run into problems later on. For right now, everything seems

I know this probably depends on how large of an object you are allocating, but I just want to know if I keep on doing things this way I will run into problems later on. For right now, everything seems fine. I am using Cocos2D.

Take this method for instance:

-(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];
    }
}

Notice how I'm allocating and deallocating checkDistance for every loop in the for loop? This method is also being called on a timer very quickly. The reason I decided to do the allocation over and over inside the loop verses outside is because the number 开发者_如何转开发of hummingBirdsMax may be 0, so it would not need to do allocation at all. I suppose I could just check if hummingBirdsMax > 0, but is the way I'm doing it make much difference?

The class that it allocates just checks a distance between to CGPoints and returns a bool.


There's nothing inherently wrong with what you're doing. There's no way to tell whether object allocation and deallocation are performance bottlenecks in your code without some work in the profiler (Shark or Instruments). As with all performance optimization decisions, there's no reason to even speculate without some data.

That said, if all the CheckDistance class does is perform a calculation, it's not really an object (i.e. that encapsulates state and exposes behavior). Why not just implement the calculation as a C function? Unlike the managed Java/C# world that many folks are now used to, not everything has to be (nor should it be) a class.

0

精彩评论

暂无评论...
验证码 换一张
取 消