码:
var start: NSDatevar time: NSTimeIntervalvar batch = 1000000var rate: Doublevar oArr = NSMutableArray(capacity: batch)start = NSDate()for i in 1..batch { oArr.addobject(i)}time = -start.timeIntervalSinceNowrate = Double(batch) / Double(time)println("NSMutableArray \(batch) appends in \(time) sec: \(rate)/sec")var sArr = Int[]()start = NSDate()for i in 1..batch { sArr += i}time = -start.timeIntervalSinceNowrate = Double(batch) / Double(time)println("Array<Int> \(batch) appends in \(time) sec: \(rate)/sec")
模拟器输出(beta 2)794%:
NSMutableArray 1000000 appends in 1.17320102453232 sec: 852368.843096295/secArray<Int> 1000000 appends in 9.31138801574707 sec: 107395.374170729/sec
模拟器中的输出(beta 3):
NSMutableArray 1000000 appends in 0.71416300535202 sec: 1400240.55083487/secArray<Int> 1000000 appends in 5.00839000940323 sec: 199664.961818569/sec
iPhone 5上的输出(iOS 7.1上的beta 3):
NSMutableArray 1000000 appends in 8.79256194829941 sec: 113732.494110367/secArray<Int> 1000000 appends in 55.6573320031166 sec: 17967.084730975/sec解决方法 更新:
使用Xcode beta-3,在MacBook Pro 2 GHz Intel Core i7上使用控制台程序,我使用原始测试得到这些数字(由于较小的语法更改,也修复了语法):
调试(-O0):
NSMutableArray 1000000 appends in 0.782136023044586 sec: 1278549.9843203/secArray<Int> 1000000 appends in 5.56463801860809 sec: 179706.208500177/sec
发布(-Os):
NSMutableArray 1000000 appends in 0.14977502822876 sec: 6676680.43081684/secArray<Int> 1000000 appends in 0.124498963356018 sec: 8032195.3937913/sec
发布(-Ofast):
NSMutableArray 1000000 appends in 0.151567995548248 sec: 6597698.91646863/secArray<Int> 1000000 appends in 0.122538030147552 sec: 8160731.80543105/sec总结
以上是内存溢出为你收集整理的如何利用Swift(beta)阵列击败NSMutableArray性能?全部内容,希望文章能够帮你解决如何利用Swift(beta)阵列击败NSMutableArray性能?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)