ios Swift 与 Objective-C:应用性能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28452862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:47:59  来源:igfitidea点击:

Swift vs Objective-C: App performance

iosobjective-cperformanceswift

提问by Black Magic

I am doing some research on Swift and its differences with Objective-C. From what I could gather, the current version of Swift is quite fast, faster even than Objective-C: see here.

我正在研究 Swift 及其与 Objective-C 的差异。据我所知,当前版本的 Swift 速度相当快,甚至比 Objective-C 还要快:请参阅此处

However, since most of these tests are done with sorting algorithms and such, I am wondering if Swift will actually be faster than Objective-C when it is used for development of iOS apps. Can anyone enlighten me on this, preferably from their own experience.

但是,由于这些测试中的大多数都是使用排序算法等完成的,我想知道 Swift 在用于开发 iOS 应用程序时是否真的会比 Objective-C 更快。任何人都可以启发我,最好是根据他们自己的经验。

回答by trimi

Swift is claimed by Apple to be faster than Objective-C, and as you said it is faster in those sorting algorithms, but for the usage of iOS development, a simple user would not recognize the difference between an app developed in Swift or Objective-C. I developed a lot of apps in Objective-C that are in apple store, and now several in Swift and so far users can not tell the difference if one is much faster than the other.

苹果声称Swift比Objective-C更快,正如你所说的在那些排序算法中它更快,但对于iOS开发的使用,一个简单的用户不会意识到用Swift或Objective开发的应用程序之间的区别 - C。我在苹果商店里用 Objective-C 开发了很多应用程序,现在有几个在 Swift 中,到目前为止用户无法分辨出一个比另一个快得多的区别。

Swift is unlikely to result in applications that run much faster than applications developed in Objective-C. Even though the two languages are quit different, both target the same Cocoa and Cocoa Touch APIs, iOS and OS X a, both are statically typed languages and both use the same LLVM compiler, so they are not that different after all. There will be performance differences, as the two languages aren't identical after all, but don't expect significant differences.

Swift 不太可能导致应用程序运行速度比用 Objective-C 开发的应用程序快得多。尽管这两种语言完全不同,但都针对相同的 Cocoa 和 Cocoa Touch API,iOS 和 OS X a,都是静态类型语言,并且都使用相同的 LLVM 编译器,因此它们毕竟没有什么不同。会有性能差异,因为这两种语言毕竟不相同,但不要指望有显着差异。

Swift is also developed from Apple to appeal to new programmers because it is similar to languages such as Ruby and Python than it is Objective-C.

Swift 也是由 Apple 开发的,以吸引新程序员,因为它类似于 Ruby 和 Python 等语言,而不是 Objective-C。

回答by Christian

There is a great blog-postabout the improvement of Swift performance especially afterthe Swift 1.2 release.

有一篇很棒的博客文章介绍了 Swift 性能的改进,尤其是Swift 1.2 发布之后。

The author ran several tests with different kind of code like Objc-like Swift code, Swift only and Objective-c only code. And the result was, that Swift 1.2 is much faster than before. He ran tests with JSON so it's a bit more practical than just algorithms.

作者用不同类型的代码运行了几次测试,比如类似 Objc 的 Swift 代码、仅 Swift 和仅 Objective-c 代码。结果是,Swift 1.2 比以前快得多。他使用 JSON 进行测试,因此它比算法更实用。

Beside the 'real' performance, my personal experience about that is, that I'm developing much easier in Swift. I never liked the .hand .mfiles from Objective-C because it stopped the 'flow of programming'. Also I think the Syntax itself is much easier than in objective-c [with these brackets].

除了“真实”的性能,我个人的经验是,我在 Swift 中的开发要容易得多。我从不喜欢Objective-C 中的.h.m文件,因为它阻止了“编程流程”。此外,我认为语法本身比objective-c [使用这些括号] 容易得多。

So I think, if you write a new Project from Scratch, Swift is much easier, faster and more elegant. (My opinion)

所以我认为,如果你从头开始编写一个新项目,Swift 会更容易、更快和更优雅。(我的看法)

回答by vesi

Swift comparing to Objective-C has its own benefits like: Swift handles strings more easily, swift tuples offer compound variables, and furthermore, coders don't need to spend time annotating variables with type information and risk making mistakes; in most cases, the compiler can infer the type from the value that a variable is being set with.

Swift 与 Objective-C 相比有其自身的优势,例如:Swift 更容易处理字符串,Swift 元组提供复合变量,此外,编码人员不需要花时间用类型信息来注释变量并冒犯错误的风险;在大多数情况下,编译器可以从变量设置的值推断类型。

回答by Joseph Lord

You can write slow code in any language and Swift is no exception. I haven't had time to fully evaluate the Swift 1.2 Beta but even before most code could be made reasonably fast but it was also very easy to make it very slow. Accessing non-final instance methods especially was very slow and Debug builds were horrifically slow (I have several cases of 100x slower than release builds). A little work to optimise the most deeply nested loops was usually enough to quickly get it somewhere close to C performance.

你可以用任何语言编写慢代码,Swift 也不例外。我还没有时间来全面评估 Swift 1.2 Beta,但即使在大多数代码可以相当快地制作之前,也很容易让它变得很慢。特别是访问非最终实例方法非常慢,并且调试构建非常慢(我有几个比发布版本慢 100 倍的情况)。优化最深嵌套循环的一些工作通常足以快速使其接近 C 性能。

Most of the code you write is not that performance critical provided you can move slow operations off the main UI queue. More time will be spent in API calls and those will not be affected by the language used to call them. Even where performance is critical the amount of code that needs to be heavily optimised will tend to be small and you could switch to a faster language (e.g. C) for just those parts.

如果您可以将慢速操作移出主 UI 队列,您编写的大多数代码对性能并不那么重要。更多的时间将花在 API 调用上,并且不会受到调用它们所用语言的影响。即使在性能至关重要的情况下,需要大量优化的代码量也往往很少,您可以只为这些部分切换到更快的语言(例如 C)。

When comparing the Objective-C it is also worth considering what we mean by Objective-C. You can write C functions in Objective-C code and they will result in code as fast as C. I would say that they were C and that to meaningfully talk about Objective-C performance it should be code based on Objective-C message sending and probably NSArrays rather than raw C arrays. If that is the basis Swift (when optimised and using structs and final classes) will come out quite well. However if you are comparing with C code it will usually be the case that Swift will be slower at the moment.

在比较 Objective-C 时,也值得考虑一下我们所说的 Objective-C。您可以在 Objective-C 代码中编写 C 函数,它们将产生与 C 一样快的代码。我会说它们是 C,为了有意义地谈论 Objective-C 性能,它应该是基于 Objective-C 消息发送和可能是 NSArrays 而不是原始 C 数组。如果这是基础 Swift(当优化并使用结构和最终类时)会很好。但是,如果您与 C 代码进行比较,通常情况下 Swift 目前会更慢。

I have a few blog posts about optimising Swift on my blogand I gave a short talkback in October.

我在我的博客上有一些关于优化 Swift 的博客文章,我在 10 月份做了一个简短的演讲

回答by Techvalens

Swift is faster compare to Objective-C; that's what Apple's Swift team claims, and it is certainly true. However, the fact is that you have to plan many things in order to write the responsive apps. Here are a few pointers:

Swift 比 Objective-C 更快;这就是 Apple 的 Swift 团队所声称的,这当然是真的。然而,事实是您必须计划很多事情才能编写响应式应用程序。这里有一些提示:

  1. Remove unused resources
  2. Optimize resources for ex images
  3. Caching
  4. Compression
  5. Reusable code
  6. Object life management
  1. 删除未使用的资源
  2. 优化前图像的资源
  3. 缓存
  4. 压缩
  5. 可重用代码
  6. 对象寿命管理