Recently, I started to study the efficiency of the string and learned about the conventional methods to improve the performance. Now consider a previous question: there is a long string, and the occurrence frequency of the letters must be counted.
My general idea is:
1. In any case, you have to repeat it once. It is best to make sure that the statistics are completed once the loop is completed.
2. Comparison is involved in the loop.
For example, if the loop encounters character f, at least the current number of times of f has a storage location. In addition, to update the current number of occurrences, first locate the corresponding location in the cache.
According to my ideas, I mainly improved efficiency in 2. Normally, one character is cyclically written, and then compared 26 times (assuming the primary lowercase letter, then update the frequency of the current character!
What I think of now is to use a hash table, which is equivalent to direct locating and Statistics! The program performance is as follows:
It is about 375 characters long and takes milliseconds. The test code is as follows: Public static void Test ()
{
DateTime a = DateTime. Now;
Console. WriteLine (string. Concat ("START", a. ToString ("yyyy-MM-dd hh: mm: ss ")));
Int lenght;
String strFilePath = "c: \ 11.txt ";
StreamReader reader = null;
Reader = new StreamReader (strFilePath );
String str = reader. ReadToEnd ();
// Increase the vertex Length
Str = string. concat (str, str, str, str );
Lenght = str. Length;
System. Collections. Hashtable ha = new System. Collections. Hashtable ();
For (int k = 0; k <26; k ++)
Ha. Add (97 + k, 0 );
For (int k = 0; k <str. Length; k ++)
{
If (ha. ContainsKey (Asc (str [k]. ToString ())))
Ha [Asc (str [k]. toString ()] = int. parse (ha [Asc (str [k]. toString ()]. toString () + 1;
}
Int max = int. Parse (ha [97]. ToString ());
String cs = "";
For (int k = 0; k <26; k ++)
{
If (int. Parse (ha [97 + k]. ToString ()> max)
{
Max = int. Parse (ha [97 + k]. ToString ());
Cs = Chr (97 + k );
}
}
DateTime B = DateTime. Now;
Console. WriteLine (string. Concat ("end:", B. ToString ("yyyy-MM-dd 24hh: mm: ss ")));
Console. writeline (string. concat ("total length is", lenght. tostring (), "the most frequently occurring character in the string is", Cs, "Number of times", Max. tostring (), "Time used", B. subtract (). milliseconds. tostring (), "millisecond "));
Console. Readline ();
}
It is estimated that there should be better algorithms.