Swift、Objective C语言性能测试

techbrood 发表于 2014-06-05 12:57:23

标签: Swift, Objective C, Benchmark, 性能测试

- +

Swift发布是苹果近来最大的一个事件,谁赢得应用开发者,谁将赢得世界。

Swift包含了很多现代语言特性尤其是从脚本语言中吸取了很多营养,此外苹果发布Swift时,使用特别选用的一些例子来宣称Swift性能对于Ojbective C的大幅提升,如复杂对象排序。

不过仔细聆听苹果的发布视频,当Craig宣称性能有提升时,掌声一片,但别着急,保持聆听,当展示第1个实例时掌声寥寥,而对于第2个性能测试实例,则未能获得掌声。

为什么呢?是例子过于特殊缺乏说服力,发布现场的人员不买账吗。

开发者Keith第一时间给出了自己的测试结果,实际数据显示,Swift在简单循环、递增、赋值、数组扩展、字符串拼接上性能远不如Objective C。可能的一个解释是Swift使用类classes,总在执行ARC(Auto Referrence Count),而Objective C则更多使用C风格的数据类型。

具体测试方法、代码和数据引用如下:

Loop a million times
Swift:  0.0036s
Objective-C:  .0021s (1.7x faster)
No work done in the inner loop, just iterate.  Swift actually performs pretty well here.  It’s a straight C exercise on the Objective-C side.  Note that in Swift I used a for loop with an index variable incremented with x = x + 1, because ++ is far slower and for _ in 0…999999 is glacial.
Increment
Swift:  0.024s
Objective-C:  0.0023s (10.4x faster)
Strangely, Swift has a major performance issue with the ++ operator.  It’s roughly 6x s l o w e r than x = x + 1, which is the basic code I used to get the best performance.
Assign
Swift:  0.024s
Objective-C:  0.0022s (10.9x faster)
This is a simple x = y.
Yeowch.  I’m guessing Automatic Reference Counting is involved on the Swift side.  Retaining and releasing a million times would bring on the hurt.
Append native string to native array
Swift:  6.49s
Objective-C:  0.046s (141.1x faster)
In Swift I used an Array of String.  In Objective-C I added an NSString to an NSMutableArray with no optimizations or tweaks.  It would be even faster if we dropped to CFMutableArrayRef because in so many cases you don’t need to retain what you add to an array, something NSMutableArray does automatically – and, behind the scenes, Swift is almost surely doing the same because of how Automatic Reference Counting works.  Straight C arrays would be blinding.  ARC is not a performance optimization.
Append native integer to native array
Swift:  6.51s
Objective-C:  0.023s (283x faster)
In Swift I used an Array of Int.  In Objective-C I added an NSNumber to an NSMutableArray with no optimizations or tweaks.  It would be even faster if we dropped to CFMutableArrayRef because in so many cases you don’t need to retain what you add to an array, something NSMutableArray does automatically – and, behind the scenes, Swift is almost surely doing the same because of how Automatic Reference Counting works.  Straight C arrays would be blinding.  ARC is not a performance optimization.
Concatenate two strings
Swift:  3.47s
Objective-C:  0.27s (21x faster)
In Swift, the inner loop looked like this:
    theString3 = theString + theString2
In Objective-C, the inner loop looked like this:
    theString3 = [theString stringByAppendingString:theString2];

循环(Loop a million times)

Swift:  0.0036s

Objective-C:  .0021s (1.7x faster)

循环里没有任何其他操作。实际上Swift在这里表现不错,因为Objective-C在这个测试用例下就好比一个简单的C语言测试,注意这里的循环方式是x=x+1

自增(Increment)

Swift:  0.024s

Objective-C:  0.0023s (10.4x faster)

奇怪的是Swift ++操作有严重的性能问题,比x=x+1慢6倍。

赋值(Assign)

Swift:  0.024s

Objective-C:  0.0022s (10.9x faster)

这只是一个简单语句 x = y.

估摸着Swift使用了ARC,保留和释放一百万次带来了性能伤害。

添加字符串到数组(Append native string to native array)

Swift:  6.49s

Objective-C:  0.046s (141.1x faster)

Swift代码使用了字符串数组(Array of String).  Objective-C中则是把一个NSString加到一个NSMutableArray中,没有启用优化和其他改造。而在Objective-C中使用CFMutableArrayRef还会更快,因为很多情况下,你不需要去保留那个字符串。

添加整数到数组(Append native integer to native array)

Swift:  6.51s

Objective-C:  0.023s (283x faster)

Swift代码使用了整型数组Array of Int.  Objective-C使用了NSNumber和NSMutableArray

拼接字符串(Concatenate two strings)

Swift:  3.47s

Objective-C:  0.27s (21x faster)

Swift内部循环代码:

    theString3 = theString + theString2

Objective-C内部循环代码:

    theString3 = [theString stringByAppendingString:theString2];

 

兼听则明,Swift还是新生儿,需要接受开发者的考验。一个明智的策略或许是对于现有的项目,保持使用Objective C,而对于新项目,尝试性使用Swift,并让开发团队跟进Swift语言发展状况,随时学习。

 

possitive(4) views18373 comments1

发送私信

最新评论

Dragon's army 2017-09-28 06:33:24

对于快速发展的东西,测试信息会过时。如果能写出测试用的版本就更好了。


请先 登录 再评论.
相关文章