What is a fast way to convert a string of two characters to an array of booleans?

前端 未结 8 1556
我寻月下人不归
我寻月下人不归 2021-02-04 10:24

I have a long string (sometimes over 1000 characters) that I want to convert to an array of boolean values. And it needs to do this many times, very quickly.

let         


        
8条回答
  •  情话喂你
    2021-02-04 10:58

    One more step should speed that up even more. Using reserveCapacity will resize the array once before the loops starts instead of trying to do it as the loop runs.

    var output = [Bool]()
    output.reserveCapacity(input.characters.count)
    for char in input.characters {
        output.append(char == "1")
    }
    

提交回复
热议问题