同步队列中的代码比异步队列中的代码运行更快.不应该相反吗? [英] Code runs faster when queued synchronously than asynchronously. Shouldn't it be the opposite?
问题描述
我正试图通过至少在两个不同的内核之间分配主线程来加快进程速度,以减慢主线程的速度.
I am trying to speed up a process that slows down my main thread by distributing it at least across two different cores.
我想我可以做到这一点的原因是,每个单独的操作都是独立的,只需要两个点和一个浮点数即可.
The reason I think I can pull this off is that each of the individual operations are independent requiring only two points and a float.
但是,当我执行queue.asnc
vs queue.sync
时,我的第一个刺入点是代码的运行速度明显变慢,而且我不知道为什么!
However my first stab at is has the code running significantly slower when doing queue.asnc
vs queue.sync
and I have no clue why!
这是同步运行的代码
var block = UnsafeMutablePointer<Datas>.allocate(capacity: 0)
var outblock = UnsafeMutablePointer<Decimal>.allocate(capacity: 0)
func initialise()
{
outblock = UnsafeMutablePointer<Decimal>.allocate(capacity: testWith * 4 * 2)
block = UnsafeMutablePointer<Datas>.allocate(capacity: particles.count)
}
func update()
{
var i = 0
for part in particles
{
part.update()
let x1 = part.data.p1.x; let y1 = part.data.p1.y
let x2 = part.data.p2.x; let y2 = part.data.p2.x;
let w = part.data.size * rectScale
let w2 = part.data.size * rectScale
let dy = y2 - y1; let dx = x2 - x1
let length = sqrt(dy * dy + dx * dx)
let calcx = (-(y2 - y1) / length)
let calcy = ((x2 - x1) / length)
let calcx1 = calcx * w
let calcy1 = calcy * w
let calcx2 = calcx * w2
let calcy2 = calcy * w2
outblock[i] = x1 + calcx1
outblock[i+1] = y1 + calcy1
outblock[i+2] = x1 - calcx1
outblock[i+3] = y1 - calcy1
outblock[i+4] = x2 + calcx2
outblock[i+5] = y2 + calcy2
outblock[i+6] = x2 - calcx2
outblock[i+7] = y2 - calcy2
i += 8
}
}
这是我在多个内核之间分配工作负载的尝试
Here is my attempt at distributing the workload among multiple cores
let queue = DispatchQueue(label: "construction_worker_1", attributes: .concurrent)
let blocky = block
let oblocky = outblock
for i in 0..<particles.count
{
particles[i].update()
block[i] = particles[i].data//Copy the raw data into a thead safe format
queue.async {
let x1 = blocky[i].p1.x; let y1 = blocky[i].p1.y
let x2 = blocky[i].p2.x; let y2 = blocky[i].p2.x;
let w = blocky[i].size * rectScale
let w2 = blocky[i].size * rectScale
let dy = y2 - y1; let dx = x2 - x1
let length = sqrt(dy * dy + dx * dx)
let calcx = (-(y2 - y1) / length)
let calcy = ((x2 - x1) / length)
let calcx1 = calcx * w
let calcy1 = calcy * w
let calcx2 = calcx * w2
let calcy2 = calcy * w2
let writeIndex = i * 8
oblocky[writeIndex] = x1 + calcx1
oblocky[writeIndex+1] = y1 + calcy1
oblocky[writeIndex+2] = x1 - calcx1
oblocky[writeIndex+3] = y1 - calcy1
oblocky[writeIndex+4] = x2 + calcx2
oblocky[writeIndex+5] = y2 + calcy2
oblocky[writeIndex+6] = x2 - calcx2
oblocky[writeIndex+7] = y2 - calcy2
}
}
我真的不知道为什么会出现这种减速!我正在使用UnsafeMutablePointer
,因此数据是线程安全的,并且我确保多个线程无法同时读取或写入任何变量.
I really have no clue why this slowdown is happening! I am using UnsafeMutablePointer
so the data is thread safe and I am ensuring that no variable can ever get read or written by multiple threads at the same time.
这是怎么回事?
推荐答案
您的期望可能是错误的.您的目标是释放主线程,然后您做到了. 那现在更快了:主线程!
Your expectations may be wrong. Your goal was to free up the main thread, and you did that. That is what is now faster: the main thread!
但是在后台线程上的async
表示请在您希望的任何旧时间执行此操作,使其暂停以使其他代码可以在其中运行" –并不意味着快速执行",而不是完全没有.而且我在您的代码中看不到任何qos
规范,所以这不像您在要求寻求特殊关注或其他任何事情.
But async
on a background thread means "please do this any old time you please, allowing it to pause so other code can run in the middle of it" — it doesn't mean "do it fast", not at all. And I don't see any qos
specification in your code, so it's not like you are asking for special attention or anything.
这篇关于同步队列中的代码比异步队列中的代码运行更快.不应该相反吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!