How to improve the performance of making a sub array in swift?

In an opengl project,the following function costs so much time during a frame,is there any way to make a sub array eficiently?


func colorArrayWithParameter(_ parameter:DrawParameter) -> [GLfloat] {

let startIndex = parameter.startIndexShift * 24

let endIndex = (parameter.endIndexShift + 1 ) * 24

let colors = [Float](self.colorBuffer[startIndex..<endIndex])

return colors

}

Replies

The most obvious option would be to have it return an array slice rather than an array. Whether that’s feasible depends on what the call sites look like.

Which speaks to a bigger point, namely that it’s going to be hard to optimise this code as it stands; rather, you’ll need to post more details about the overall context.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

The self.colorBuffer is a const instance of [GLfloat] . Maybe array slice is not so Efficient way to create a sub array. I'm going to use C language to improve the performance.