Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

c# - Fastest way to remove chars from string

I have a string from which I have to remove following char: ' ', ' ', and ''. I have tried three different ways of removing these char and benchmarked them so I can get the fastest solution.

Following are the methods and there execution time when I ran them 1000000 times:

It should be fastest solution if I have 1 or 2 char to remove. But as I put in more char, it starts to take more time

str = str.Replace("
", string.Empty).Replace("
", string.Empty).Replace("", string.Empty);

Execution time = 1695

For 1 or 2 char, this was slower then String.Replace, but for 3 char it showed better performance.

string[] split = str.Split(new char[] { '', '
', '
' }, StringSplitOptions.None);
str = split.Aggregate<string>((str1, str2) => str1 + str2);

Execution time = 1030

The slowest of all, even with 1 char. Maybe my regular expression is not the best.

str = Regex.Replace(str, "[
]", string.Empty, RegexOptions.Compiled);

Execution time = 3500

These are the three solutions I came up with. Is there any better and faster solution that anyone here know, or any improvements I can do in this code?

String that I used for benchmarking:

StringBuilder builder = new StringBuilder();
        builder.AppendFormat("{0}
{1}
{2}
{3}
{4}
{5}
{6}
{7}
{8}
{9}",
         "SELECT ",
         "[Extent1].[CustomerID] AS [CustomerID], ",
         "[Extent1].[NameStyle] AS [NameStyle], ",
         "[Extent1].[Title] AS [Title], ",
           "[Extent1].[FirstName] AS [FirstName], ",
           "[Extent1].[MiddleName] AS [MiddleName], ",
           "[Extent1].[LastName] AS [LastName], ",
           "[Extent1].[Suffix] AS [Suffix], ",
           "[Extent1].[CompanyName] AS [CompanyName], ",
           "[Extent1].[SalesPerson] AS [SalesPerson], ");
        string str = builder.ToString();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's the uber-fast unsafe version, version 2.

    public static unsafe string StripTabsAndNewlines(string s)
    {
        int len = s.Length;
        char* newChars = stackalloc char[len];
        char* currentChar = newChars;

        for (int i = 0; i < len; ++i)
        {
            char c = s[i];
            switch (c)
            {
                case '
':
                case '
':
                case '':
                    continue;
                default:
                    *currentChar++ = c;
                    break;
            }
        }
        return new string(newChars, 0, (int)(currentChar - newChars));
    }

And here are the benchmarks (time to strip 1000000 strings in ms)

    cornerback84's String.Replace:         9433
    Andy West's String.Concat:             4756
    AviJ's char array:                     1374
    Matt Howells' char pointers:           1163

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...