Two swift functions increasing compiling time dram

2019-05-26 02:36发布

After going back and reading my app's build logs, there seems to be a strange issue where two (relatively) simple functions are both increasing the compiling time by one minute each (58 & 53 seconds respectively). This can be seen in my build logs below:

enter image description here

These functions are in my CAAgeViewController and both reference a UISlider in my storyboard. They make sure that both the max and min sliders are at most within 1 year of each other, and either function sets a label to "18-24 Years" or something to that respect. They are as follows:

@IBAction func minAgeChanged(_ sender: UISlider) {
    if round(minAgeSlider.value / 1) < round(maxAgeSlider.value / 1) - 1 {
        minAgeSlider.value = round(minAgeSlider.value / 1)
        ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"
    } else {
        minAgeSlider.value = round(maxAgeSlider.value / 1) - 1
        ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"

    }
}

@IBAction func maxAgeChanged(_ sender: UISlider) {
    if round(maxAgeSlider.value / 1) > round(minAgeSlider.value / 1) + 1 {
        maxAgeSlider.value = round(maxAgeSlider.value / 1)
        ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"
    } else {
        maxAgeSlider.value = round(minAgeSlider.value / 1) + 1
        ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"
    }
}

I am unsure what exactly is going wrong here. Any help is appreciated!

3条回答
戒情不戒烟
2楼-- · 2019-05-26 02:53

Your problem is the chained-plus. (It's always chained-plus; ok, not always but always…)

ageLabel.text = String(Int(round(minAgeSlider.value / 1))) + "-" + String(Int(round(maxAgeSlider.value / 1))) + " Years"

Replace this with:

ageLabel.text = "\(Int(round(minAgeSlider.value)))-\(Int(round(maxAgeSlider.value))) Years"

I'm fairly certain the /1 isn't helping you here. round + Int should do everything you meant.

查看更多
霸刀☆藐视天下
3楼-- · 2019-05-26 03:02

The most likely cause of the slowdown is the way that you're concatenating strings with +. For some reason that I haven't been able to figure out, it's much faster (at least in Swift 2/3) to append strings with "\(string1) \(string2)" than to use string1 + string2. Same goes for arrays and their + operator. We saw a 100x decrease in compile time when we changed how we're doing concatenation.

查看更多
ら.Afraid
4楼-- · 2019-05-26 03:16

Have you tried removing the division by 1 and casting your values to Double instead? They may already be Doubles, mind you. You may just need to call round().

查看更多
登录 后发表回答