optimization - What is the Fastest Way to Check for a Keyword in a List of Keywords in Delphi? -


I have a small list of keywords that I should really have done:

  Case 'Child' Myken word: (code for CHIL); 'HUSB': (code for HUSB); 'Wife': (code for wife); 'Sex': (code for sx); Other (code for everything); End;  

Unfortunately, the CASE statement can not be used for string.

If I can directly use, for example:

  if MyKeyword = 'CHIL' then (code for CHIL) otherwise if MyKeyword = 'HUSB' Then (code for Huesby) and if the mickeyword = 'wif' then (the code for the wife) otherwise if the mikeen = 'sx' then (code for sx) other (code for all else);  

But I heard it is relatively incompetent.

What I was doing instead:

  P: = pos ('' + + + '' + '', 'CHL Hesab vive sex'); P: of case 1 (code for CHIL); 6: (code for HUSB); 11: (code for wife); 17: (code for sex); Other (code for everything); End;  

This is definitely not the best programming style, but it works fine for me and there has not been any difference till now.

So what is the best way to re-write it in Delphi is that it is both simple, understandable but also fasting?

(For reference, I am using Delphi 2009 with Unicode strings.)


Followup:

Toby recommends I have to use that if I then build another. See on my examples that use a CASE statement, I can see how this is a reliable answer. Unfortunately, my inadvertently my real question was hidden by my inclusion of the case.

I really do not care what that keyword is. This is just a bonus if specific method can identify it like POS method. What do I need to know if the keyword is in the set of keywords

In fact, I want to know if there is something better than this:

  if pos ( '' + MyKeyword + '', 'CHIL HUSB WIFE SEX') & gt; 0]  

If this is not a better homogenous then:

  if (MyKeyword = 'CHIL') or (MyKeyword = 'HUSB') or (MyKeyword = 'wifi') or (MyKeyword = 'sex')  

In Barry's comment to the question of Cornell, he mentioned TDictionary generic I have not yet had the new generic collections Not picked up and it seems that I should hide in them. My question here would be whether they have been created for efficiency and how to use TDS and how to do it compared to the speed above?


Later in profiling, I have found that in the form of strings: ('' + MyKeyword + '') is very expensive time-wise and should be avoided whenever possible. Almost any other solution is better than doing this.

Mostly I use the index text function from the strings for that purpose. This is similar to your approach, but the return value is independent of the unmarried length of the strings. As a gimmick, it is also insensitive (if you do not want use indexStr).

  function index text (const acrylic: string; constant avalues: string of arrays): integer; Surcharge;  

The comment in these works actually refers to the creation of the case:

{ANSI match text & amp; AnsiIndexText provides the case as a task to deal with the string}


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -