i have code, call "checkingfunction" function. not using threading in app, love use if benefits performance of app.
the "checkingfunction", takes more time expected. takes more 30 seconds complete execution. cant wait long in app. not good, in middle of game.
somebody me out here rewrite function, can execute in faster way. functional programming way, if possible.
func returncharactersfromafourletterstring(inputstring : string) -> (first : character,second : character, third : character, fourth : character) { return (inputstring[advance(inputstring.startindex, 0)],inputstring[advance(inputstring.startindex, 1)],inputstring[advance(inputstring.startindex, 2)],inputstring[advance(inputstring.startindex, 3)]) } func checkingwords(userenteredword : string) { var tuplefourletters = self.returncharactersfromafourletterstring(userenteredword) var firstletter = string(tuplefourletters.first) var secondletter = string(tuplefourletters.second) var thirdletter = string(tuplefourletters.third) var fourthletter = string(tuplefourletters.fourth) var mainarrayofwords : [string] = [] // array contains around 0.2 million words var userenteredthesewords : [string] = [] // array contains less 10 elements // check firstletter index in 0..<array.count // array of letters strings , count = 200 { var input = array[index] var firstword = "\(input)\(secondletter)\(thirdletter)\(fourthletter)" var secondword = "\(firstletter)\(input)\(thirdletter)\(fourthletter)" var thirdword = "\(firstletter)\(secondletter)\(input)\(fourthletter)" var fourthword = "\(firstletter)\(secondletter)\(thirdletter)\(input)" if !contains(userenteredthesewords, firstword) && !contains(userenteredthesewords, secondword) && !contains(userenteredthesewords, thirdword) && !contains(userenteredthesewords, fourthword) { if contains(mainarrayofwords, firstword ) { self.delegate?.wordmatchedfromdictionary(firstword) return } else if contains(mainarrayofwords, secondword) { self.delegate?.wordmatchedfromdictionary(secondword) return } else if contains(mainarrayofwords, thirdword) { self.delegate?.wordmatchedfromdictionary(thirdword) return } else if contains(mainarrayofwords, fourthword) { self.delegate?.wordmatchedfromdictionary(fourthword) return } } if index == array.count - 1 { self.delegate?.wordmatchedfromdictionary("noword") } } }
input of function 4 letter word, inside function changing each letter looping through 200 letters, , checking in mainarray that, whether of these changed words exists in mainarray. if exists, return me word, otherwise return noword. totally, can see checking contains(mainarray, word) thing around 800 times, think line consumes more time, cause mainarray contains 0.2 million words.
use dictionaries things.
when measure times, swift code, measure release build, not debug build. on other hand, measure on slowest device capable of running code.
Comments
Post a Comment