Objective C 中的分配和释放速度有多快?
我知道这可能取决于您分配的对象有多大,但我只想知道我是否继续这样做,以后会遇到问题。目前看来,一切都很好。我用的是Cocos2D。
以这个方法为例:
-(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];
}
}
注意我如何为 for 循环中的每个循环分配和取消分配 checkDistance 吗?这个方法也很快被定时器调用。我之所以决定在循环内部和外部进行一遍又一遍的分配,是因为 hummingBirdsMax 的数量可能为 0,所以根本不需要进行分配。我想我可以检查 hummingBirdsMax 是否 > 0,但是我这样做的方式有很大不同吗?
它分配的类只是检查 CGPoints 之间的距离并返回一个布尔值。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你所做的事情本质上并没有什么问题。如果没有分析器(Shark 或 Instruments)中的一些工作,就无法判断对象分配和释放是否是代码中的性能瓶颈。与所有性能优化决策一样,没有理由在没有数据的情况下进行推测。
也就是说,如果
CheckDistance
类所做的只是执行计算,那么它并不是真正的对象(即封装状态并公开行为)。为什么不直接将计算实现为 C 函数呢?与许多人现在习惯的托管 Java/C# 世界不同,并不是所有东西都必须(也不应该是)类。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.