A quick look at the string

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:


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:

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。


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:

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?

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。


  • 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 Gerber drop-off:R0uter's Blog » A quick look at the string

Reproduced Please keep the source and description link:https://www.logcg.com/archives/2348.html

About the Author


The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。


Leave a Reply

Your email address will not be published. Required fields are marked *