How to make Playground execution time is as fast as if we run in iOS application

后端 未结 1 1575
我寻月下人不归
我寻月下人不归 2021-01-02 23:07

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 {
           


        
1条回答
  •  醉梦人生
    2021-01-02 23:36

    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

    or go to menu:

    View > Navigators > Show project navigator

    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++
            }
        }
    }
    

    Then it's as usual in the Playground itself:

    let startTime = NSDate()
    
    PlayGround.count()
    
    let endTime = NSDate()
    
    let interval = endTime.timeIntervalSinceDate(startTime) // 0.0062
    

    0 讨论(0)
提交回复
热议问题