c# - Inverse String.Replace - Faster way of doing it? -
I have a method to change each character, which I specify. For example, replace
("Stop Testing; Or, No", ".; / \\". ToCharArray (), '*'); will come back
"****. *****; ***, ****".
Now, this is not an example of premature adaptation. I call this method several times during the network operation. I think that now on the wire, this is causing some latency, and it helped a bit by removing it. Any help to increase this speed will be appreciated.
public static string replacement (this string origin, four [] pattern, four replacement) {int index = 0; Int old = -1; Stringbuilder sb = New stringbilder (original Lang); While ((index = original indexfoxy (pattern, index) gt; -1) {sb.Append (new string (substitution, index - old -1)); Sb.Append (original [index]); Old = pointer ++; } If (original.lumbi - old> gt; 1) {sb.Append (new string (replacement, original Lang - (old + 1));} return sb.ToString ();} Last # 'I added a test case for a 3K character string, to see how well each of these measurements was run 100K instead of 1x. The only surprise was that Regular expression has increased 'better than others', but it is not any help because it can not It is very slow to start:
user short * 1m long * 100k scale jon 3192125 6.66 Luke 360 2659 7.3 9 Guffa 409 2827 6.91 My 447 3372 7.54 DirkGently 1094 9134 8.35 Michael 1591 12785 8.04 Peter 21106 94386 4.47
UPDATE: I created a regular expression for the regular version of Peter, and it was compacted to be set fair:
User Short * 1m long * 100cm Peter 8997 74715 8.30
Pastebin link with my test code, please Correct me if this is wrong:
OK, but a ~ 60 kb string , It will perform approximately 40% faster than your version:
public static string replacement (this string root, four [] pattern, four replacement) {int index = 0; Stringbilder sb = new stringbilder (new string (replacement, original length)); While ((index = original.indexfeni (pattern, index) gt; -1) {SB [index] = original [index ++]; } Return sb.ToString (); } This trick is initializing a new string with all replacement characters, since most of them will be replaced.
Comments
Post a Comment