Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster than String.Replace()

Tags:

performance

c#

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();

        }
    }
}
like image 652
mjb Avatar asked Jun 28 '12 01:06

mjb


1 Answers

    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();
like image 55
Serj-Tm Avatar answered Oct 14 '22 08:10

Serj-Tm