Most of the time,We need to look at many strings,To determine the specific content filtering out。For example, through the input method which pocketed,We need to use auxiliary code word to filter out candidates need。
In General,Find and Compare certainly figures to fastest,However, in the lexicon we can not put all the words are converted to digital (although theoretically feasible ......) search on the string,We realize there are many ways to,Here, I say my own ideas:
Set<String>
Since my thesaurus secondary screening is effective only for code words or word vocabulary,So I consider them as a set (Set) to detect,The vocabulary is split into words。I'm sorry,It does not say split the cost of a separate,The hash table set that will soon set,But speed is not the line,It is possible that a large number of advantages when it,But unfortunately,Its speed is the slowest。
I used a brief collection of statistical time then to repeatedly traverse,The results obtained are as follows:
1 2 3 4 5 6 7 |
let ptime = Date().timeIntervalSince1970 let b:Set<String> = ["a","b","c","d","e","f","g","h"] for _ in (0...9999999) { _=b.contains("hb") } let during = Date().timeIntervalSince1970 - ptime print(during) |
result 8.90186786651611 It is worth mentioning that,Whether it is hit or missing,Speed is the same。of course,Slow string comparison also have to consider their own questions of cost。
a.contains(“a”)
It comes with a string containing the method,It can detect a string a Whether to include the "a" ,If you include the,Returns true 。This is a very direct way:
1 2 3 4 5 6 7 |
let a = "abcdefgh" let ptime = Date().timeIntervalSince1970 for _ in (0...9999999) { a.contains("a") } let during = Date().timeIntervalSince1970 - ptime print(during) |
The results obtained with this simple traversal 7.06464505195618 second。
So it can not be faster? We change the thinking,If we just look,That fact, there is a way to consider,That is
a.range(of: “a”)
Swift 中 String There is also a built-in program,That is to get the specified range of characters in another string,It used to extract the string alive modification,If you do not,It returns nil 。
Here we use the return value to determine whether an empty string contains,Speed is how much?
1 2 3 4 5 6 7 |
let a = "abcdefgh" let ptime = Date().timeIntervalSince1970 for _ in (0...9999999) { _ = a.range(of: "ah") } let during = Date().timeIntervalSince1970 - ptime print(during) |
Implementation of the results is 6.38309478759766 second。Obviously,Find the speed of this method is better than the top two kinds of specialized search much faster。
Summarize
- What method is a means not an end,In short we can get the desired results like,Which is better? The fastest;
- A plurality of functions similar to each other can be an alternative way to do the test,We can not rely on guessing to determine the performance;
- Set is the only hit and lose speed consistent approach,but range It is lost faster than the Set;
- These results are based on the string to find,Not all cases :)
Original article written by LogStudio:R0uter's Blog » A quick look at the string
Reproduced Please keep the source and description link:https://www.logcg.com/archives/2348.html
Comments