如何使Playground执行时间与在iOS应用程序中运行一样快 [英] How to make Playground execution time is as fast as if we run in iOS application

查看:102
本文介绍了如何使Playground执行时间与在iOS应用程序中运行一样快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到游乐场的执行速度不可靠。例如,使用代码:

I see that playground execution speed is not reliable. For example with a code:

import UIKit
var count = 0;

let startTime = NSDate()
for i in 1...10000 {
   count++
}
let endTime = NSDate()

let interval = endTime.timeIntervalSinceDate(startTime)

interval的值大约是2s,这是不可靠的。
随着Swift 2.0&的推出XCode beta 7,是否可以像在iOS应用程序中那样快速地执行游戏代码?

the value of interval is about 2s, which is not reliable. With the release of Swift 2.0 & XCode beta 7, is it possible to make swift playground code execute as fast as in iOS application?

推荐答案

有一个解决方法,感谢Playground的 Sources 文件夹。

There's a workaround thanks to the Sources folder of the Playground.

您可以使用菜单添加外部文件:

You can either use the menu to add external files:


新建>向源添加文件

New > Add files to sources

或转到菜单:


查看>导航器>显示项目导航器

View > Navigators > Show project navigator

并在 Sources 文件夹中删除 .swift 文件。

and drop a .swift file in the Sources folder.

要访问此文件夹中的代码必须公开:

To be accessible, your code in this folder has to be public:

public class PlayGround {
    public class func count() {
        var count = 0
        for i in 1...10000 {
            count++
        }
    }
}

然后就像Playground本身一样:

Then it's as usual in the Playground itself:

let startTime = NSDate()

PlayGround.count()

let endTime = NSDate()

let interval = endTime.timeIntervalSinceDate(startTime) // 0.0062

这篇关于如何使Playground执行时间与在iOS应用程序中运行一样快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆