iPhone Performance Killers

本文探讨了Objective-C中 autorelease 与 alloc/release 的性能对比,并通过具体示例展示了如何优化代码以提高帧率,尤其是在资源受限的旧设备上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Have a look at the following code, and then answer these questions before reading on:

  1. Which function will run faster?
  2. What will be the framerate for each function when run 100 times per frame on an iPhone 3G?
  3. Will wrapping the 100 calls to function1 in an NSAutoreleasePool show any difference?

[cc lang="ObjC" height="465"]
-(void) function1
{
CGPoint pos = [self position];
id x = [NSNumber numberWithFloat:pos.x];
id y = [NSNumber numberWithFloat:pos.y];
id objects = [NSArray arrayWithObjects:x, y, nil];
id keys = [NSArray arrayWithObjects:@"x", @"y", nil];
id dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
dict; // avoid compiler warning, is a noop
}

-(void) function2
{
CGPoint pos = [self position];
id x = [[NSNumber alloc] initWithFloat:pos.x];
id y = [[NSNumber alloc] initWithFloat:pos.y];
id objects = [[NSArray alloc] initWithObjects:x, y, nil];
id keys = [[NSArray alloc] initWithObjects:@”x”, @”y”, nil];
id dict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
[x release];
[y release];
[objects release];
[keys release];
[dict release];
}
[/cc]

The Answers

  1. Which function will run faster? Answer: function1
  2. What will be the framerate for each function when run 100 times per frame on an iPhone 3G?Answer: 27 fps for function1 and 24 fps for function2.
  3. Will wrapping the 100 calls to function1 in an NSAutoreleasePool show any difference? Answer:no, but memory of temporary objects is released immediately.

Needless to say, on an iPod (4th Generation) and an iPad these tests all run at 60 fps and give no indication whatsoever that the performance on an iPhone 3G would suffer this much (and neither does the Simulator, of course). All the more reason to test early and often on older devices.

To autorelease or not?

Common wisdom may tell you that alloc/release is faster than autorelease. Even Apple recommends avoiding autorelease, right?

Not quite, because this is often misunderstood: Apple recommends to avoid autorelease but only for functions which create a lot of temporary objects and because of the constrained memory – not because it’s slow or even dangerous – autorelease is not dangerous.

Since memory is so constrained on 1st and 2nd generation iOS devices, it’s best to release that memory as soon as possible and don’t leave it allocated for longer than necessary. To achieve this, you can choose to do two things in this case: use alloc/release or enclose the loop in an NSAutoreleasePool. The latter option is preferred since it will release the memory right away, and not some time later. And autorelease is generally preferable because you will never, ever forget to send a release message to an object – which means it’ll be leaked and forever use up memory.

You can write well-performing, even better-performing code by using autorelease and using NSAutoreleasePool around tight loops creating many temporary autorelease objects.

Innocent looking code kills framerate

Did you expect that creating 100 rather simple NSDictionary instances each frame would drag the framerate down to around 24-27 fps? Me neither. I knew the code wasn’t going to be blazing fast, but I never expected it to have such an impact. However, it can be optimized somewhat since I’m unnecessarily creating two NSArray instances to hold the keys and values respectively before using them to create the NSDictionary. In fact we can get rid of them by using dictionaryWithObjectsAndKeys and doing this in a single step:

[cc lang="ObjC"]
-(void) function1Optimized
{
CGPoint pos = [self position];
id x = [NSNumber numberWithFloat:pos.x];
id y = [NSNumber numberWithFloat:pos.y];
id dict = [NSDictionary dictionaryWithObjectsAndKeys:x, @"x", y, @"y", nil];
dict; // avoid compiler warning, is a noop
}
[/cc]

Sometimes it helps to look around what other ways there are to run the same code. In terms of performance this is an order of a magnitude faster and now clocks in at 42 fps. Still not good enough for realtime rendering obviously but an improvement of over 50% by cutting two NSArray allocations is a very simple and effective optimization.

Just as a general guideline, when I get rid of the two NSNumber instances and simply pass empty strings for x and y the framerate went back up to 60 fps. Of course that’s over-optimizing to the point where the code doesn’t work anymore. It just goes to show how expensive the creation of NSDictionary and NSArray are, as is wrapping simple types in NSNumber or NSValue objects.

If you can avoid allocation and temporary objects, avoid it. If you can’t, at least avoid creating temporary objects every frame. Re-use objects as much as possible. Unfortunately, that’s not an option for NSNumber objects since you can’t change the value of a NSNumber instance.

基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明,该项目是个人毕设项目,答辩评审分达到98分,代码都经过调试测试,确保可以运行!欢迎下载使用,可用于小白学习、进阶。该资源主要针对计算机、通信、人工智能、自动化等相关专业的学生、老师或从业者下载使用,亦可作为期末课程设计、课程大作业、毕业设计等。项目整体具有较高的学习借鉴价值!基础能力强的可以在此基础上修改调整,以实现不同的功能。 基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指挥8种手势识别源码+数据集+模型+详细项目说明基于pytorch实现中国交通警察指
内容概要:本文档详细介绍了Python反爬虫技术的各种应对策略,包括基础和高级方法。基础部分涵盖User-Agent伪装、IP代理池、请求频率控制等,其中涉及使用fake_useragent库随机生成User-Agent、设置HTTP/HTTPS代理、通过随机延时模拟正常访问行为。动态页面处理方面,讲解了Selenium和Pyppeteer两种自动化工具的使用,可以用于加载并获取JavaScript渲染后的网页内容。对于验证码问题,提供了OCR识别简单验证码、Selenium模拟滑块验证码操作以及利用第三方平台破解复杂验证码的方法。登录态维持章节介绍了如何通过Session对象保持登录状态,并且演示了Cookie的保存与读取。数据加密对抗部分探讨了JavaScript逆向工程和WebAssembly破解技巧,如使用PyExecJS执行解密脚本。最后,高级反爬绕过策略中提到了WebSocket数据抓取和字体反爬解析,确保能够从各种复杂的网络环境中获取所需数据。 适合人群:有一定Python编程经验,从事数据采集工作的开发人员。 使用场景及目标:①帮助开发者理解并掌握多种反爬虫绕过技术;②为实际项目中的数据抓取任务提供有效的解决方案;③提高爬虫程序的成功率和稳定性。 其他说明:在学习过程中,建议结合具体案例进行实践,同时注意遵守网站的robots协议及相关法律法规,合法合规地进行数据采集活动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值