Swift游乐场与字典太慢

时间:2015-10-23 20:25:45

标签: swift swift-playground

我在Xcode 7.1中使用以下代码创建了一个Swift游乐场,其评估速度非常慢(通常只需几分钟)。为什么会这样?由于它使操场基本上无法使用,我可以做任何事情来改善其执行时间吗?

let sets = [
    "TEMPERATURE":[
        (value:37.0, attributes:["A":0.0, "B":0.2]),
        (value:37.5, attributes:["A":0.0, "B":1.0]),
        (value:38.0, attributes:["A":0.2, "B":0.5]),
        (value:38.5, attributes:["A":0.5, "B":0.2]),
        (value:39.0, attributes:["A":0.8, "B":0.0]),
        (value:39.5, attributes:["A":1.0, "B":0.0]),
        (value:40.0, attributes:["A":1.0, "B":0.0]),
    ],
    "VARIATION":[
        (value:0.0, attributes:["A":0.0, "B":1.0]),
        (value:2.0, attributes:["A":0.2, "B":0.8]),
        (value:5.0, attributes:["A":0.5, "B":0.5]),
        (value:8.0, attributes:["A":0.8, "B":0.2]),
        (value:10.0, attributes:["A":1.0, "B":0.0]),
    ]
]

2 个答案:

答案 0 :(得分:3)

这里的问题是Swift编译器在推断sets的类型时非常慢。如果你明确地使用类型,那么Swift编译器不必花费任何时间来推断它。试试这个:

let sets: [String:[(value: Double, attributes:[String:Double])]] = [
    "TEMPERATURE":[
        (value:37.0, attributes:["A":0.0, "B":0.2]),
        (value:37.5, attributes:["A":0.0, "B":1.0]),
        (value:38.0, attributes:["A":0.2, "B":0.5]),
        (value:38.5, attributes:["A":0.5, "B":0.2]),
        (value:39.0, attributes:["A":0.8, "B":0.0]),
        (value:39.5, attributes:["A":1.0, "B":0.0]),
        (value:40.0, attributes:["A":1.0, "B":0.0]),
    ],
    "VARIATION":[
        (value:0.0, attributes:["A":0.0, "B":1.0]),
        (value:2.0, attributes:["A":0.2, "B":0.8]),
        (value:5.0, attributes:["A":0.5, "B":0.5]),
        (value:8.0, attributes:["A":0.8, "B":0.2]),
        (value:10.0, attributes:["A":1.0, "B":0.0]),
    ]
]

答案 1 :(得分:0)

尝试在"VARIATION"初始化后添加sets。如果这仍然很慢,请继续缩小它以将较小和较小的位附加到较大的字典。我知道这很乏味,但不幸的是,这可能是Xcode和大量数据的常见错误。