版本1:
func longTask(for myData:Data){
let tick = Date() // startTime
let fileDataLength = myData.count
let count = 0
while count < fileDataLength{
//task 1
task1Result = task1()
//task 2
task2Result = task2(task1Result)
//task 3
task3 result = task3(task1Result)
count = count + incrementCount
}
NSLog("%f",tick.timeInterverSinceNow) // print out -0.06
}
第2版:
func longTask(for myData: Data ){
let fileDataLength = myData.count
let count = 0
let testQueue = OperationQueue()
while count < fileDataLength{
//task 1
task1Result = task1()
{
// store all needed value
let name:String = task1Result.mane
...
testQueue.addOperation{ // do in other thread
//task 2
task2Result = task2(name:name , ...)
//task 3
task3 result = task3(....)
}
}()
count = count + incrementCount
}
testQueue.waitUntilAllOperationsAreFinished()
NSLog("%f",tick.timeInterverSinceNow) // print out -0.5
}
在版本2中,我想象如果 longTask 由线程A运行,则将任务2和3移动到线程B ,而循环将执行下一步 task1( )线程A中的,而线程B运行task2()和task3()。这将减少执行时间,但版本2比版本1慢大约10倍。我在这里失踪了什么?
答案 0 :(得分:0)
您的策略似乎没问题,但我们对您的真实代码一无所知。在你的游乐场试试这个
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
import Foundation
let q = OperationQueue()
let start = Date()
print("start at", start)
let n = 4
var gs = [Int]()
for j in 0..<n {
gs.append(0)
//q.addOperation {
var s = 0
for i in 0..<1000 {
s += i
}
gs[j] = s
let t = Date()
OperationQueue.main.addOperation {
let pt = Date()
print(s, j, "calculated:", t.timeIntervalSince(start), "printed:", pt.timeIntervalSince(start))
}
//}
}
q.waitUntilAllOperationsAreFinished()
print(gs, "GRANT TOTALL:", gs.reduce(0, +), "finished in", Date().timeIntervalSince(start), "seconds\n")
在我自己的环境中
处理器名称:Intel Core i5
处理器速度:2,6 GHz
处理器数量:1
核心总数:2
L2缓存(每个核心):256 KB
L3缓存:3 MB
内存:16 GB
打印
start at 2017-04-24 11:47:03 +0000
[499500, 499500, 499500, 499500] GRANT TOTALL: 1998000 finished in 0.327317953109741 seconds
499500 0 calculated: 0.0641489624977112 printed: 0.419835984706879
499500 1 calculated: 0.130355000495911 printed: 0.420383989810944
499500 2 calculated: 0.230241000652313 printed: 0.420827984809875
499500 3 calculated: 0.326637983322144 printed: 0.421335995197296
但在OperationQueue的帮助下
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
import Foundation
let q = OperationQueue()
let start = Date()
print("start at", start)
let n = 4
var gs = [Int]()
for j in 0..<n {
gs.append(0)
q.addOperation {
var s = 0
for i in 0..<1000 {
s += i
}
gs[j] = s
let t = Date()
OperationQueue.main.addOperation {
let pt = Date()
print(s, j, "calculated:", t.timeIntervalSince(start), "printed:", pt.timeIntervalSince(start))
}
}
}
q.waitUntilAllOperationsAreFinished()
print(gs, "GRANT TOTALL:", gs.reduce(0, +), "finished in", Date().timeIntervalSince(start), "seconds\n")
打印
start at 2017-04-24 11:58:45 +0000
[499500, 499500, 499500, 499500] GRANT TOTALL: 1998000 finished in 0.186473965644836 seconds
499500 0 calculated: 0.0701659917831421 printed: 0.388468980789185
499500 1 calculated: 0.131179988384247 printed: 0.389105975627899
499500 2 calculated: 0.165158987045288 printed: 0.389568984508514
499500 3 calculated: 0.18574994802475 printed: 0.389993965625763
正如所料,代码运行得更快
gs数组怎么样?它是线程安全吗?没有!!所以最后我们需要添加一些同步,即使在我们非常简单的例子中似乎不需要...
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
import Foundation
let q = OperationQueue()
let start = Date()
print("start at", start)
let n = 4
var gs = [Int]()
let queue = DispatchQueue(label: "MyArrayQueue", attributes: .concurrent)
for j in 0..<n {
queue.async(flags: .barrier) {
gs.append(0)
}
q.addOperation {
var s = 0
for i in 0..<1000 {
s += i
}
queue.async(flags: .barrier) {
gs[j] = s
}
let t = Date()
OperationQueue.main.addOperation {
let pt = Date()
print(s, j, "calculated:", t.timeIntervalSince(start), "printed:", pt.timeIntervalSince(start))
}
}
}
q.waitUntilAllOperationsAreFinished()
var mgs:[Int] = []
queue.sync {
mgs = gs
}
print(mgs, "GRANT TOTALL:", mgs.reduce(0, +), "finished in", Date().timeIntervalSince(start), "seconds\n")
最终结果
start at 2017-04-24 14:38:12 +0000
[499500, 499500, 499500, 499500] GRANT TOTALL: 1998000 finished in 0.164229989051819 seconds
499500 0 calculated: 0.0660730004310608 printed: 0.324115991592407
499500 1 calculated: 0.0995810031890869 printed: 0.324918031692505
499500 2 calculated: 0.126493990421295 printed: 0.325313985347748
499500 3 calculated: 0.162481009960175 printed: 0.32580304145813