Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为什么创建一个合适大小的Swift数组会降低速度?_Arrays_Swift_Performance_Vector_Microbenchmark - Fatal编程技术网

Arrays 为什么创建一个合适大小的Swift数组会降低速度?

Arrays 为什么创建一个合适大小的Swift数组会降低速度?,arrays,swift,performance,vector,microbenchmark,Arrays,Swift,Performance,Vector,Microbenchmark,更新:我在这个测量中犯了一个错误,使用了调试模式。在发布模式下,我得到了预期的结果 我需要将大量整数附加到Swift数组中: let numBytes = 12 * 1048576 * 3 let start = Date() var appendingImage = [UInt8]() for i in 0..<numBytes { appendingImage.append(UInt8(i % 256)) } print("Append took \(-start.timeI

更新:我在这个测量中犯了一个错误,使用了调试模式。在发布模式下,我得到了预期的结果


我需要将大量整数附加到Swift数组中:

let numBytes = 12 * 1048576 * 3

let start = Date()
var appendingImage = [UInt8]()
for i in 0..<numBytes {
  appendingImage.append(UInt8(i % 256))
}

print("Append took \(-start.timeIntervalSinceNow)")
让numBytes=12*1048576*3
让我们开始=日期()
var appendingImage=[UInt8]()

对于0中的i..如果您使用的是什么级别的优化?无法在Mac上复制(在发布模式下)。我测量附加0.137秒和预创建0.05秒。你是对的-这是调试模式造成的工件。它在发布模式下按预期工作。两条评论都投了赞成票。现在,问题是您是否发布了您自己问题的答案-我希望这是正确的(因为它分配了足够的内存?)-并进行了很好的解释,还是您应该删除/删除该问题。@dfd我在开始时编辑了该问题。看一看。
var precreatedImage = Array(repeating: UInt8(0), count: numBytes)
for i in 0..<numBytes {
  precreatedImage[i] = UInt8(i % 256)
}
print("Precreated took \(-start.timeIntervalSinceNow)")