Skip to content

Instantly share code, notes, and snippets.

@ignazioc
Last active November 16, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ignazioc/29caa6610ff14de7c005ba922c243697 to your computer and use it in GitHub Desktop.
Save ignazioc/29caa6610ff14de7c005ba922c243697 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// BackgroundExample
//
// Created by Calo, Ignazio on 16.11.18.
// Copyright © 2018 eBay Kleinanzeigen. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myDataQueue = DispatchQueue(label: "DataQueue", qos: .userInitiated, attributes: .concurrent, autoreleaseFrequency: .workItem, target: nil)
var datapoints1 = [Int]()
var datapoints2 = [Int]()
let start1 = Date()
for i in 1...50000 {
myDataQueue.async(flags: .barrier) {
datapoints1.append(i)
if datapoints1.count == 50000 {
print("Asynch completed in \(Date().timeIntervalSince(start1))")
}
}
}
let end1 = Date()
print("Concurrent background Queue takes \(end1.timeIntervalSince(start1)) to create all the tasks")
let start2 = Date()
for i in 1...50000 {
datapoints2.append(i)
}
let end2 = Date()
print("Main Queue takes \(end2.timeIntervalSince(start2))")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment