Last active
November 16, 2018 15:14
-
-
Save ignazioc/29caa6610ff14de7c005ba922c243697 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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