About string Efficiency

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.