How to speed up this method that removes text from a string?

I wrote the following method to remove the namespace in parentheses from strings.

I would like to make it as fast as possible.

Is there a way to speed up the following code?

using System; namespace TestRemoveFast { class Program { static void Main(string[] args) { string[] tests = { "{http://company.com/Services/Types}ModifiedAt", "{http://company.com/Services/Types}CreatedAt" }; foreach (var test in tests) { Console.WriteLine(Clean(test)); } Console.ReadLine(); } static string Clean(string line) { int pos = line.IndexOf('}'); if (pos > 0) return line.Substring(pos + 1, line.Length - pos - 1); else return line; } } } 
+6
performance c # text
source share
11 answers

You can try parallelism, as this does not look like synchronous cure. A parallel foreach with PLINQ would do the trick.

But if you can’t wait until VS2010 is officially released, you can try Poor Man Parallel.ForEach Iterator by Emre Aydinceren

+3
source share

The approach seems quiet fast. But from the line you have, I conclude that the name is usually less than {URL}. You can use the .LastIndexOf () method. I think it starts at the end of the line

+3
source share

Let's say you find the answer here. I think you need to think about what the β€œsolution” will look like for the next guy who is looking at your code.

I'll take a more readable code and a couple of milliseconds any day.

+2
source share

Have you tried this with Regex and / or used stringbuilder instead of string?

+1
source share

If you changed the speed for a space, you can loop once in the given array and copy characters other than "{. *}". This would save two calls (.IndexOf () and .Substring ()).

+1
source share

Are you sure this is a bottleneck in your code? What is your specification for the time requirements of this method? Have you profiled the code that you have to see if it meets these specifications?

I would suggest that the code you have is almost optimal. Both IndexOf and Substring call unsafe code to do all kinds of fancy optimizations that won't be available to you unless you also follow the unsafe route. If you do, you'll end up rewriting IndexOf and Substring .

So, if this code is not the final bottleneck in your code, and you have a reasonable specification of the time requirements for this method, I would focus your efforts elsewhere.

+1
source share

The slowest thing there, of course, is Console.WriteLine (). Take the following example:

  public void TestCleanSpeed() { var start = DateTime.Now; for (var i = 0; i < 10000; i++) { string[] tests = { "{http://company.com/Services/Types}ModifiedAt", "{http://company.com/Services/Types}CreatedAt" }; foreach (var test in tests) { Console.WriteLine(Clean(test)); } } var end = DateTime.Now; var ts = end - start; Console.WriteLine(ts); } 

If you run it as is, it takes almost six seconds. Then remove Console.WriteLine and instead assign var newTest = Clean(test); . In my test, for 10,000 executions, it took less than 0.02 seconds.

0
source share

The only other approach is to use line.Remove(0, pos + 1); but I think that internally this Remove is more complicated than Substring, because Remove can also cut something from the middle.

So, the substring () must be voice.

0
source share

You can convert your string to XName and get the name section as follows.

 ((System.Xml.Linq.XName)"{http://company.com/Services/Types}ModifiedAt").LocalName 
0
source share
 line.Substring(line.IndexOf('}') + 1); 

Marginally faster.

0
source share

instead of your foreach ...

 for( int i=0;i<tests.Length;i++) Console.WriteLine( tests[i].Replace("}",null ) ); 
-one
source share

All Articles