gpt4 book ai didi

ios - 为什么这段代码每秒运行 1000 次时运行速度这么慢?

转载 作者:行者123 更新时间:2023-11-30 13:25:35 25 4
gpt4 key购买 nike

所以我有这段代码,可以为 GPU 构建带有顶点数据的数组。目前,我可以拥有的粒子数量受到 CPU 的严重限制,特别是此功能。

这个函数基本上根据“粒子”对象数组中的值覆盖预先分配的数组中的值(我认为比附加更有效)。

    circles = [GLfloat](count:  ((MAX_PARTICLES) * 8) * 2, repeatedValue: 0)
squares = [GLfloat](count: ((MAX_PARTICLES) * (6 * 4)), repeatedValue: 0)
indices = [GLushort](count: ((MAX_PARTICLES) * 6), repeatedValue: 0)

令人惊讶的是,并不是 obj.update(1/60.0) 调用导致了这种延迟,并且“calcRectangle”似乎运行得尽可能快。他们有什么办法可以加速这段代码,或者这是现代 swift 的极限吗?

    private func _buildArrays1()
{
var circlePos:Int = 0
var rectPos:Int = 0
var buffer_index:Int = 0
var accum:Int = 0
for obj in particles
{
obj.update(1.0 / 60.0)
let pos:Point = obj.position
let lpos:Point = obj.lastPosition
var color:ColorHSV = obj.color
let width:GLfloat = obj.size
let rect = Math.makeRectangle(pos, p2: lpos, w: width)

color = ColorHSV(h: 1.0, s: 0.0, v: 0.0, a: 1.0)
circles[circlePos] = pos.x
circles[circlePos + 1] = pos.y
circles[circlePos + 2] = 0.0
circles[circlePos + 3] = color.h
circles[circlePos + 4] = color.s
circles[circlePos + 5] = color.v
circles[circlePos + 6] = color.a
circles[circlePos + 7] = width
circles[circlePos + 8] = lpos.x
circles[circlePos + 9] = lpos.y
circles[circlePos + 10] = 0.0
circles[circlePos + 11] = color.h
circles[circlePos + 12] = color.s
circles[circlePos + 13] = color.v
circles[circlePos + 14] = color.a
circles[circlePos + 15] = width
circlePos += 16

color = ColorHSV(h: 1.0, s: 1.0, v: 0.0, a: 1.0)

squares[rectPos] = rect.0.x; rectPos += 1;
squares[rectPos] = rect.0.y; rectPos += 1;
squares[rectPos] = color.h; rectPos += 1;
squares[rectPos] = color.s; rectPos += 1;
squares[rectPos] = color.v; rectPos += 1;
squares[rectPos] = color.a; rectPos += 1;
squares[rectPos] = rect.1.x; rectPos += 1;
squares[rectPos] = rect.1.y; rectPos += 1;
squares[rectPos] = color.h; rectPos += 1;
squares[rectPos] = color.s; rectPos += 1;
squares[rectPos] = color.v; rectPos += 1;
squares[rectPos] = color.a; rectPos += 1;
squares[rectPos] = rect.2.x; rectPos += 1;
squares[rectPos] = rect.2.y; rectPos += 1;
squares[rectPos] = color.h; rectPos += 1;
squares[rectPos] = color.s; rectPos += 1;
squares[rectPos] = color.v; rectPos += 1;
squares[rectPos] = color.a; rectPos += 1;
squares[rectPos] = rect.3.x; rectPos += 1;
squares[rectPos] = rect.3.y; rectPos += 1;
squares[rectPos] = color.h; rectPos += 1;
squares[rectPos] = color.s; rectPos += 1;
squares[rectPos] = color.v; rectPos += 1;
squares[rectPos] = color.a; rectPos += 1;

indices[buffer_index] = GLushort(accum); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 1); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 2); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 3); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 2); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 1); buffer_index += 1;


accum += 4;
}
}

以防万一(虽然我怀疑问题就在这里),这里是数学课的相关摘录。

class Math {
static func makeRectangle(p1: Point, p2: Point, w: GLfloat) -> (Point, Point, Point, Point)
{
//Returns the vertices of a rectangle made with the two point
if ((p2.x < p1.x) && (p2.y < p1.y))
{
//POINTs are not in the right order
return makeRectangle(p2, p2: p1, w: w)
}
let vector = subtract(p2, p1)
let unit = devide(vector, length(vector))
let perp = Point(x: -unit.y, y: unit.x)
let calc = multiply(perp, w / 2)
return (add(p1, calc), subtract(p1, calc), add(p2, calc), r: subtract(p2, calc))
}
static func subtract(l:Point, _ r:Point) -> Point
{
return Point(x: l.x - r.x, y: l.y - r.y)
}
static func add(l:Point, _ r:Point) -> Point
{
return Point(x: r.x + l.x, y: r.y + l.y)
}
static func devide(l:Point, _ r:GLfloat) -> Point
{
return Point(x: l.x / r, y: l.y / r)
}
static func length(o:Point) -> GLfloat
{
return sqrt(o.x * o.x + o.y * o.y)
}
static func multiply(o:Point, _ v:GLfloat) -> Point
{
return Point(x: o.x * v, y: o.y * v)
}
}

最佳答案

好吧,当我坐在一个讲座中(我完全在听)时,我意识到,为什么我将所有内容都保存在结构中,为什么不直接在 GPU 的数组中保存位置。

所以我摆脱了“Point”结构并让一切都基于数组位置!现在瓶颈在GPU!

将值存储在结构中,然后将它们逐个组件复制到数组中,效率很低。

关于ios - 为什么这段代码每秒运行 1000 次时运行速度这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com