在iPhone开发中创建自动发布对象 [英] Creating autorelease objects in iPhone Development

查看:89
本文介绍了在iPhone开发中创建自动发布对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一些NSDecimalNumber对象对象作为我的应用程序的一部分(因为我需要它们提供的计算精度),但是我注意到在计算中它们返回了NSDecimalNumber对象,这些对象大概是自动释放的.

I have a requirement to create some NSDecimalNumber objects objects as part of my application (as I require the precision of calculation they offer) but I note that in calculations they return NSDecimalNumber objects which are, presumably, autoreleased.

我的问题确实是这在iPhone应用程序中是否可能存在问题,在该应用程序中我可能会进行大量计算.

My question is really whether this is potentially problematic in an iPhone application where I may carry out lots of calculations.

这个问题不仅与NSDecimalNumber有关,而且与在开发iPhone应用程序过程中有时不可避免地创建自动释放对象有关.

The question is not just relating to NSDecimalNumber specifically but to the sometimes unavoidable creation of autoreleased objects in the course of developing an iPhone application.

任何有关此点的详细答案将不胜感激.

Any detailed answers on this point would be gratefully received.

推荐答案

是的,在iPhone上创建大量自动发布的实例会造成内存问题,特别是在紧密循环中,这就是为什么我倾向于在可能的情况下避免它们的原因.您可以创建自己的自动发布池来管理此操作,但是它们也会增加一些性能开销和其他必须跟踪的代码.

Yes, creating lots of autoreleased instances on the iPhone can create memory problems, particularly in a tight loop, which is why I tend to avoid them when I can. You can create your own autorelease pools to manage this, but they also add some performance overhead and additional code that you have to keep track of.

正因为如此,当我进行高精度计算时,我倾向于使用NSDecimal C结构而不是NSDecimalNumbers.实际上,我对此进行了一些基准测试,发现使用C结构时性能得到了显着提高(摘自我的回答

It is for this reason that when I do high-precision calculations, I tend to use the NSDecimal C struct instead of NSDecimalNumbers. In fact, I performed some benchmarks on this and found a significant performance increase when going with the C struct (copied from my answer here):

NSDecimal

Additions per second: 3355476.75
Subtractions per second: 3866671.27
Multiplications per second: 3458770.51
Divisions per second: 276242.32

NSDecimalNumber

Additions per second: 676901.32
Subtractions per second: 671474.6
Multiplications per second: 720310.63
Divisions per second: 190249.33

如您所见,NSDecimal路径和NSDecimalNumber路径之间的计算速度几乎提高了五倍. NSDecimal和NSDecimalNumber计算之间的最大区别是NSDecimalNumber实例的内存分配.因此,应避免在任何可能的地方分配临时自动释放的实例.

As you can see, there is almost a fivefold increase in calculation speed between the NSDecimal paths and NSDecimalNumber ones. The biggest difference between the NSDecimal and NSDecimalNumber calculations was the memory allocation of the NSDecimalNumber instances. Therefore, you should avoid allocating temporary autoreleased instances wherever you can.

这篇关于在iPhone开发中创建自动发布对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆