Swift vs Objective-C:App性能

时间:2022-12-04 21:19:21

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更快。任何人都可以在此赐教,最好是根据自己的经验。

5 个解决方案

#1


26  

There is a great blog-post about the improvement of Swift performance especially after the 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-like 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 .h and .m files 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)

所以我认为,如果你从Scratch编写一个新项目,Swift会更容易,更快速,更优雅。 (我的想法)

#2


34  

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。

#3


0  

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进行全面评估,但即使在大多数代码可以合理快速地制作之前,它也很容易让它变得非常慢。访问非最终实例方法特别是非常慢,并且Debug构建非常慢(我有几个案例比发布版本慢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 blog and I gave a short talk back in October.

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

#4


0  

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.

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

#5


-1  

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:

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

  1. Remove unused resources
  2. 删除未使用的资源
  3. Optimize resources for ex images
  4. 优化ex图像的资源
  5. Caching
  6. 高速缓存
  7. Compression
  8. 压缩
  9. Reusable code
  10. 可重复使用的代码
  11. Object life management
  12. 对象生活管理

#1


26  

There is a great blog-post about the improvement of Swift performance especially after the 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-like 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 .h and .m files 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)

所以我认为,如果你从Scratch编写一个新项目,Swift会更容易,更快速,更优雅。 (我的想法)

#2


34  

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。

#3


0  

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进行全面评估,但即使在大多数代码可以合理快速地制作之前,它也很容易让它变得非常慢。访问非最终实例方法特别是非常慢,并且Debug构建非常慢(我有几个案例比发布版本慢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 blog and I gave a short talk back in October.

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

#4


0  

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.

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

#5


-1  

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:

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

  1. Remove unused resources
  2. 删除未使用的资源
  3. Optimize resources for ex images
  4. 优化ex图像的资源
  5. Caching
  6. 高速缓存
  7. Compression
  8. 压缩
  9. Reusable code
  10. 可重复使用的代码
  11. Object life management
  12. 对象生活管理