Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:13:04+00:00 2026-06-06T17:13:04+00:00

Is there any other method that is faster than doing like this? private void

  • 0

Is there any other method that is faster than doing like this?

private void EscapeStringSequence(ref string data)
{
    data = data.Replace("\\", "\\\\"); // Backslash
    data = data.Replace("\r", "\\r");  // Carriage return
    data = data.Replace("\n", "\\n");  // New Line
    data = data.Replace("\a", "\\a");  // Vertical tab
    data = data.Replace("\b", "\\b");  // Backspace
    data = data.Replace("\f", "\\f");  // Formfeed
    data = data.Replace("\t", "\\t");  // Horizontal tab
    data = data.Replace("\v", "\\v");  // Vertical tab
    data = data.Replace("\"", "\\\""); // Double quotation mark
    data = data.Replace("'", "\\'");   // Single quotation mark
}

— Edited (Add explanation) —
Q1: Is there a reason why you need to speed it up? Is it causing a huge problem?
This part is used in this project: http://mysqlbackuprestore.codeplex.com/
I’m going to loop lots of various length of strings into this function repeatly. The whole process takes around 6-15 seconds to finished for millions of rows. There are other part get involve too. I’m trying to speed up every part.

Q2: How slow is it now?
OK, I’ll capture the exact time used and post it here. I’ll come back later. (will post the result tomorrow)

Update 29-06-2012
I have run test. This is the result:

Speed Test: String.Replace() – measured in miliseconds
Test 1: 26749.7531 ms
Test 2: 27063.438 ms
Test 3: 27753.8884 ms
Average: 27189.0265 ms
Speed: 100%

Speed Test: Foreach Char and Append – measured in miliseconds
Test 1: 8468.4547 ms
Test 2: 8348.8527 ms
Test 3: 8353.6476 ms
Average: 8390.3183 ms
Speed: 224% < faster
===================================
Update – Next Test (Another round)
===================================
——
Test Replace String Speed.
Test 1: 26535.6466
Test 2: 26379.6464
Test 3: 26379.6463
Average: 26431.6464333333
Speed: 100%
——
Test Foreach Char String Append.
Test 1: 8502.015
Test 2: 8517.6149
Test 3: 8595.6151
Average: 8538.415
Speed: 309.56%
——
Test Foreach Char String Append (Fix StringBuilder Length).
Test 1: 8314.8146
Test 2: 8330.4147
Test 3: 8346.0146
Average: 8330.41463333333
Speed: 317.29%

Conclusion:
Using Foreach Char Loop and Append is faster than String.Replace().

Thanks you very much guys.

——–
Below are the codes that I used to run the test: (edited)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey();
            Console.Write("\r\nProcess started.");
            Test();
            Console.WriteLine("Done.");
            Console.Read();
        }

        public static Random random = new Random((int)DateTime.Now.Ticks);

        public static string RandomString(int size)
        {
            StringBuilder sb = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                sb.Append(ch);
            }
            return sb.ToString();
        }

        public static void Test()
        {
            string text = "\\_\r\n\a\b\f\t\v\"'" + RandomString(2000) + "\\_\r\n\a\b\f\t\v\"'" + RandomString(2000);

            List<TimeSpan> lstTimeUsed = new List<TimeSpan>();

            int target = 100000;

            for (int i = 0; i < 3; i++)
            {
                DateTime startTime = DateTime.Now;
                for (int j = 0; j < target; j++)
                {
                    if (j.ToString().EndsWith("000"))
                    {
                        Console.Clear();
                        Console.WriteLine("Test " + i.ToString());
                        Console.WriteLine(j.ToString() + " of " + target.ToString());
                    }

                    string data = text;

                    data = data.Replace("\\", "\\\\"); // Backslash
                    data = data.Replace("\r", "\\r");  // Carriage return
                    data = data.Replace("\n", "\\n");  // New Line
                    data = data.Replace("\a", "\\a");  // Vertical tab
                    data = data.Replace("\b", "\\b");  // Backspace
                    data = data.Replace("\f", "\\f");  // Formfeed
                    data = data.Replace("\t", "\\t");  // Horizontal tab
                    data = data.Replace("\v", "\\v");  // Vertical tab
                    data = data.Replace("\"", "\\\""); // Double quotation mark
                    data = data.Replace("'", "\\'");   // Single quotation mark

                }
                DateTime endTime = DateTime.Now;
                TimeSpan ts = endTime - startTime;
                lstTimeUsed.Add(ts);
            }

            double t1 = lstTimeUsed[0].TotalMilliseconds;
            double t2 = lstTimeUsed[1].TotalMilliseconds;
            double t3 = lstTimeUsed[2].TotalMilliseconds;
            double tOri = (t1 + t2 + t3) / 3;

            System.IO.TextWriter tw = new System.IO.StreamWriter("D:\\test.txt", true);
            tw.WriteLine("------");
            tw.WriteLine("Test Replace String Speed. Test Time: " + DateTime.Now.ToString());
            tw.WriteLine("Test 1: " + t1.ToString());
            tw.WriteLine("Test 2: " + t2.ToString());
            tw.WriteLine("Test 3: " + t3.ToString());
            tw.WriteLine("Average: " + tOri.ToString());
            tw.WriteLine("Speed: 100%");
            tw.Close();

            lstTimeUsed = new List<TimeSpan>();

            for (int i = 0; i < 3; i++)
            {
                DateTime startTime = DateTime.Now;
                for (int j = 0; j < target; j++)
                {
                    if (j.ToString().EndsWith("000"))
                    {
                        Console.Clear();
                        Console.WriteLine("Test " + i.ToString());
                        Console.WriteLine(j.ToString() + " of " + target.ToString());
                    }

                    string data = text;

                    var builder = new StringBuilder();
                    foreach (var ch in data)
                    {
                        switch (ch)
                        {
                            case '\\':
                            case '\r':
                            case '\n':
                            case '\a':
                            case '\b':
                            case '\f':
                            case '\t':
                            case '\v':
                            case '\"':
                            case '\'':
                                builder.Append('\\');
                                break;
                            default:
                                break;
                        }
                        builder.Append(ch);
                    }

                }
                DateTime endTime = DateTime.Now;
                TimeSpan ts = endTime - startTime;
                lstTimeUsed.Add(ts);
            }

            t1 = lstTimeUsed[0].TotalMilliseconds;
            t2 = lstTimeUsed[1].TotalMilliseconds;
            t3 = lstTimeUsed[2].TotalMilliseconds;

            tw = new System.IO.StreamWriter("D:\\test.txt", true);
            tw.WriteLine("------");
            tw.WriteLine("Test Foreach Char String Append. Test Time: " + DateTime.Now.ToString());
            tw.WriteLine("Test 1: " + t1.ToString());
            tw.WriteLine("Test 2: " + t2.ToString());
            tw.WriteLine("Test 3: " + t3.ToString());
            tw.WriteLine("Average: " + ((t1 + t2 + t3) / 3).ToString());
            tw.WriteLine("Speed: " + ((tOri) / ((t1 + t2 + t3) / 3) * 100).ToString("0.00") + "%");
            tw.Close();

            lstTimeUsed = new List<TimeSpan>();

            for (int i = 0; i < 3; i++)
            {
                DateTime startTime = DateTime.Now;
                for (int j = 0; j < target; j++)
                {
                    if (j.ToString().EndsWith("000"))
                    {
                        Console.Clear();
                        Console.WriteLine("Test " + i.ToString());
                        Console.WriteLine(j.ToString() + " of " + target.ToString());
                    }

                    string data = text;

                    var builder = new StringBuilder(data.Length + 20);
                    foreach (var ch in data)
                    {
                        switch (ch)
                        {
                            case '\\':
                            case '\r':
                            case '\n':
                            case '\a':
                            case '\b':
                            case '\f':
                            case '\t':
                            case '\v':
                            case '\"':
                            case '\'':
                                builder.Append('\\');
                                break;
                            default:
                                break;
                        }
                        builder.Append(ch);
                    }

                }
                DateTime endTime = DateTime.Now;
                TimeSpan ts = endTime - startTime;
                lstTimeUsed.Add(ts);
            }

            t1 = lstTimeUsed[0].TotalMilliseconds;
            t2 = lstTimeUsed[1].TotalMilliseconds;
            t3 = lstTimeUsed[2].TotalMilliseconds;

            tw = new System.IO.StreamWriter("D:\\test.txt", true);
            tw.WriteLine("------");
            tw.WriteLine("Test Foreach Char String Append (Fix StringBuilder Length). Test Time: " + DateTime.Now.ToString());
            tw.WriteLine("Test 1: " + t1.ToString());
            tw.WriteLine("Test 2: " + t2.ToString());
            tw.WriteLine("Test 3: " + t3.ToString());
            tw.WriteLine("Average: " + ((t1 + t2 + t3) / 3).ToString());
            tw.WriteLine("Speed: " + ((tOri) / ((t1 + t2 + t3) / 3) * 100).ToString("0.00") + "%");
            tw.Close();

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:13:06+00:00Added an answer on June 6, 2026 at 5:13 pm
        var builder = new StringBuilder(data.Length + 20);
        foreach (var ch in data)
        {
          switch (ch)
          {
            case '\\':
            case '\r':
            ...
              builder.Append('\\');
              break;
          }
          builder.Append(ch);
        }
        return builder.ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way (3rd party product or other method) to do a partial
Are there any other methods of bringing a control to the front other than
I want to know if there are any methods other than file.open(); in <windows.h>
Is there any other better way to fill up the array like : var
Are there any other packages for doing Venn diagrams in R besides the limma
Is there any other command for redirecting a controller to a particular view page
is there any other simple,nicer way? require 'pp' a1 = [02/28/10,Webinars,131,0,26 Feb 2010,0,3d, 8h,
Is there any other way in java to implement call backs apart from inner
Is there any other implementation (e.g. in an OSS project) of a Java SecurityManager
When doing a simple performance measurement, I was astonished to see that calling String.IndexOf(char)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.