gpt4 book ai didi

ios - 优化 Swift 字典分配性能

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:11 26 4
gpt4 key购买 nike

我一直在分析我的代码,发现超过 50% 的运行时间花在了在 Swift 字典中赋值的一行中。

Running Time    Self (ms)       Symbol Name
10675.0ms 51.0% 9.0 Swift.Dictionary.subscript.setter (A) -> B?

这是这个函数的第二行

func isConsistent<V, D>(variable: V, value: D, assignment: Dictionary<V, D>, csp: CSP<V,D>) -> Bool {
var tempAssignment = assignment
tempAssignment[variable] = value

这是一个中等大小程序中的一行(需要很长的时间(在我的 1.7 ghz i7 上大约需要 80 秒)来执行)并且这一行的调用频率并不比大多数其他行高。我怎样才能加快速度?

最佳答案

字典是值类型,这意味着每次将它分配给另一个变量时都会复制它尝试删除分配语句

    func isConsistent<V, D>(variable: V, value: D, assignment: Dictionary<V, D>, csp: CSP<V,D>) -> Bool {
//var tempAssignment = assignment//remove this line
assignment[variable] = value // use assignment instead

关于ios - 优化 Swift 字典分配性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31621769/

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