Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

enter image description here

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?

like image 465
Duyen-Hoa Avatar asked Jun 16 '15 14:06

Duyen-Hoa


People also ask

How long does it take to finish Swift Playgrounds?

As an aspiring app developer, you'll learn how Swift works with data and Swift playgrounds. You'll also adopt a protocol-oriented programming mindset as you work on developing apps on iPhone, Mac, or iPad. It takes about three to four learning hours to complete this course.

How do I see execution time in Xcode?

Select Product->Profile then select Time Profiler.

What is playground in IOS?

Swift Playgrounds is a revolutionary app for iPad and Mac that makes it fun to learn and experiment with Swift — a powerful programming language created by Apple and used by the pros to build today's most popular apps. Swift Playgrounds requires no coding knowledge, so it's perfect for students just starting out.

Is Swift Playgrounds a good way to learn to code?

Swift Playgrounds is a beautiful looking environment to learn to code. It's a nice, modern, object-oriented language and I can learn concepts and conventions and put them to work later in Xcode. Children can choose step-by-step lessons and puzzles to build their coding knowledge and skills.


1 Answers

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
like image 119
Eric Aya Avatar answered Sep 22 '22 09:09

Eric Aya