π₯
This file contains hidden or 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
s.resource_bundles = { | |
'ThisIsATestBundle' => ['SampleProject/Assets/*.xcassets'] | |
} |
This file contains hidden or 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
import Foundation | |
fileprivate class ThisClass {} | |
public struct Resources { | |
public static var bundle: Bundle { | |
return Bundle(for: ThisClass.self) | |
} | |
} |
This file contains hidden or 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
s.resources = [ | |
'<YOUR_PROJECT_NAME>/Assets/*.png', # for all pngs | |
'<YOUR_PROJECT_NAME>/Assets/*.m4a', # for all audios | |
# and so on... | |
] |
This file contains hidden or 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
for _ in 1...10 { | |
let loopCount = 50000 | |
measure(label: "normal array", { | |
var testArrayA: [Int] = [] | |
for _ in 1...loopCount { | |
testArrayA.append(1) | |
_ = testArrayA.last | |
} | |
}, complete: { time in normalArrayConsumeTimes.append(time) }) |
This file contains hidden or 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
for _ in 1...10 { | |
let loopCount = 50000 | |
measure(label: "normal array", { | |
var testArrayA: [Int] = [] | |
for _ in 1...loopCount { | |
self.testArrayA.append(1) | |
} | |
}, complete: { time in normalArrayConsumeTimes.append(time) }) |
This file contains hidden or 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
for _ in 1...10 { | |
let loopCount = 50000 | |
measure(label: "normal array", { | |
var testArrayA: [Int] = [] | |
for _ in 1...loopCount { | |
_ = testArrayA.last | |
} | |
}, complete: { time in normalArrayConsumeTimes.append(time) }) |
This file contains hidden or 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
extension Array where Element == TimeInterval { | |
func averageTime() -> TimeInterval { | |
return reduce(0, +) / TimeInterval(count) | |
} | |
} | |
var normalArrayConsumeTimes: [TimeInterval] = [] | |
var threadSafeArrayConsumeTimes: [TimeInterval] = [] | |
var testArrayA: [Int] = [] | |
var testArrayB = ThreadSafeArray<Int>() |
This file contains hidden or 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
let safeArray = SafeArray<Int>() | |
DispatchQueue.concurrentPerform(iterations: 10, execute: { i in | |
DispatchQueue.concurrentPerform(iterations: 10, execute: { j in | |
safeArray.append(i*j) | |
}) | |
}) |
This file contains hidden or 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
// For read | |
extension SafeArray { | |
var first: Element? { | |
var result: Element? | |
queue.sync { result = array.first } | |
return result | |
} | |
} | |
// For write |
This file contains hidden or 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
class SafeArray<Element> { | |
private let queue = DispatchQueue(label: "io.safe.array.queue", attributes: .concurrent) | |
private var array = Array<Element>() | |
} |