Delphi – In vs. array vs. Pos

I have a hobby of solving ciphers. I also solve and create crosswords. I have several tools I’ve written using the Delphi programming language to assist me in these hobbies. It recently occurred to me that there may be faster ways of doing certain functions I need, such as character counts. So I decided to test three methods of determining whether a given character is contained in a word, phrase, or collection of text characters. For purposes of my test, I decided to test whether a given character is a lower case letter in the English alphabet (a through z). I tested a loop containing five characters a,z,m,n, and 7 which I designated as ch1 through ch5;

The first method is to use Delphi’s reserved word ‘in’. This is a set function. To use this you must first create the comparison set. So define alph as the set [a]+[b]+[c], etc. Note that these elements are not ordered. “In” is a boolean function, so the statement ch1 in alph  will return a true or false.

The second method is to use an array. Define the array as an array of characters with an  index from 1 to 26 (or 0 to 25 if you prefer). Use the statement for i2 := 1 to 26 do if alph2[i2]>ch1 then break else if alph2[i2]=ch1 then …; You don’t want the loop to keep searching after it has found the character, so that is the reason for the first if clause. I tested the method both ways, using that if clause and not using it.

The last method is the string function Pos. This returns the position of a character or substring in another string, or, if it is not there, a 0. To use it define the comparison string S1 as ‘abcdefghijklmnopqrstuvwxyz’ and the statement Pos(ch1,S1)>0 will return a true or false.

I ran a loop of 100,000 iterations testing each of the five characters using each of the methods. The first method was the fastest at 15 microseconds. The array with the first if clause was next at 16 microseconds. Third was the array method without the first if clause at 31 microseconds, and last was the Pos method at 78 seconds. Although the set method is the fastest, it has a big drawback. It doesn’t tell you where in the set the character appears. Sets in general are also limited to types byte or character meaning only 255 elements. The array method is almost as fast and has neither of these limitations. It is not as powerful, however, as the Pos method because that method can test not just characters but substrings of any length, such as whole words and it identifies where it occurs if it’s there. This is probably the most often needed of the three. All three methods can be useful, but they are best suited to different purposes.

Leave a Reply

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