我看到游乐场的执行速度不可靠.例如,使用代码:
import UIKit var count = 0; let startTime = NSDate() for i in 1...10000 { count++ } let endTime = NSDate() let interval = endTime.timeIntervalSinceDate(startTime)
间隔的值约为2s,这是不可靠的.
随着Swift 2.0&的发布XCode beta 7,是否可以像在iOS应用程序中一样快速地执行swift playground代码?
解决方法
由于Playground的Sources文件夹,有一个解决方法.
New > Add files to sources
或转到菜单:
View > Navigators > Show project navigator
public class PlayGround { public class func count() { var count = 0 for i in 1...10000 { count++ } } }
然后就像Playground本身一样:
let startTime = NSDate() PlayGround.count() let endTime = NSDate() let interval = endTime.timeIntervalSinceDate(startTime) // 0.0062