3

I have a function that fixed non-printable characters in C# for JavaScript. But it works very slow! How to increase speed of this function?

private static string JsStringFixNonPrintable(string Source)
    {
        string Result = "";
        for (int Position = 0; Position < Source.Length; ++Position)
        {
            int i = Position;
            var CharCat = char.GetUnicodeCategory(Source, i);
            if (Char.IsWhiteSpace(Source[i]) ||
                CharCat == System.Globalization.UnicodeCategory.LineSeparator ||
                CharCat == System.Globalization.UnicodeCategory.SpaceSeparator) { Result += " "; continue; }
            if (Char.IsControl(Source[i]) && Source[i] != 10 && Source[i] != 13) continue;
            Result += Source[i];
        }
        return Result;
    }
1

3 Answers 3

5

I have recoded your snippet of code using StringBuilder class, with predefined buffer size... that is much faster than your sample.

    private static string JsStringFixNonPrintable(string Source)
    {
        StringBuilder builder = new StringBuilder(Source.Length); // predefine size to be the same as input
        for (int it = 0; it < Source.Length; ++it)
        {
            var ch = Source[it];
            var CharCat = char.GetUnicodeCategory(Source, it);
            if (Char.IsWhiteSpace(ch) ||
                CharCat == System.Globalization.UnicodeCategory.LineSeparator ||
                CharCat == System.Globalization.UnicodeCategory.SpaceSeparator) { builder.Append(' '); continue; }
            if (Char.IsControl(ch) && ch != 10 && ch != 13) continue;
            builder.Append(ch);
        }
        return builder.ToString();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of concatenating to the string, try using System.Text.StringBuilder which internally maintains a character buffer and does not create a new object every time you append.

Example:

StringBuilder sb = new StringBuilder();
sb.Append('a');
sb.Append('b');
sb.Append('c');
string result = sb.ToString();
Console.WriteLine(result); // prints 'abc'

Comments

0

Use Stringbuilder

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

and replace characters in-place, that should speed up things

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.