Swift: Vroom vroom fast playgrounds

Read more about Swift Playgrounds in Playground Secrets and Power Tips

You may have already discovered that while playgrounds are cool, convenient, and fun, they’re not exactly speedy. The playground’s rich interactive environment and copious feedback mechanisms produce significant overhead.

A playground’s biggest computation load is its logging. Eliminate that feedback and you can make your playground zing with efficiency. Here’s how.

Beta 3 introduced separate source files: files that are embedded into the playground bundle, and which do not require a module framework. These files aren’t just convenient for clearing up the workspace presentation, they also provide a way to significantly reduce execution overhead.

Consider the following code. When executed in my playground, it took almost 36 seconds to run.

func slow () {
    var a = 0
    for i in 0...100000 {a = a + 1}

By moving this function to a bundled Swift source file and calling it from there, that time shrank down to just 0.0059 seconds compared to the 35.9876 seconds when defined in the playground. Here’s how I timed that execution.

public func timetes(block:()->()){
    let date = NSDate()
    let timeInterval = NSDate().timeIntervalSinceDate(date)
    println("Elapsed time: \(timeInterval)")

Ages ago, I implemented a playground spirograph system that, quite honestly, took forever to run. Today, I updated that project to use separate source. Instead of spirographs that each took minutes to create, updates were near instantaneous. The following video shows my playground in action.


  • Could you maybe share the playground? Put it on GitHub or something?

  • I purchased your book through iBooks. I really appreciate the coverage of Xcode 7 and Swift 2. Have you had any success with using your own embedded Swift framework in a workspace together with a playground? The documentation suggests it’s possible. The playground sees my framework but doesn’t seem to see any of the public classes inside of it.