MonoTouch - foreach 与 for 循环(性能)
通常我很清楚这样的考虑是不成熟的优化。现在我有一些事件处理程序附加在 foreach 循环内。我想知道这种风格是否容易由于创建闭包而导致泄漏或内存使用效率低下。这种想法有道理吗?
Normally I'm well aware that a consideration like this is premature optimization. Right now I have some event handlers being attached inside a foreach loop. I am wondering if this style might be prone to leaks or inefficient memory use due to closures being created. Is there any validity to this thinking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当您的事件处理程序是匿名方法(包括但不限于 lambda 表达式)时,闭包才适用。如果是这种情况,您可能会遇到问题。但只要您在适当的时间删除这些事件处理程序就应该没问题。
closures only apply if your event handlers are anonymous methods (including, but not limited to, lambda expressions). If this is the case, you might have a problem. But it should be okay as long as you remove these event handlers at the proper time.
如果您谈论的是这样的事情:
那么答案是性能并不明显(无论如何,在我的 Monotouch 经验中),因为编译器只是使用与 Microsoft C# 编译器相同的方法创建一个类。
我在 Monotouch 中遇到的最大性能瓶颈与 SQLite 相关,以及解析日期时间。其他一切,包括复杂的 LINQ 语句,都在 3GS 上运行 - 我不确定 AOT 编译器执行了什么魔法,但我只会担心它是否会花费 0.5 秒或更多秒来执行任务。
If you are talking about something like this:
Then the answer is the performance is not noticeable (in my Monotouch experience anyway) as the compiler simply creates a class with a method the same way the Microsoft C# compiler.
The biggest performance bottlenecks I've encountered in Monotouch have been SQLite related, and parsing DateTimes. Everything else, including complex LINQ statements fly on the 3GS - I'm not sure what magic is performed by the AOT compiler but I would only worry if it creeps up into 0.5 or more seconds to perform the task.