I wrote the following method to remove the namespace in brackets from strings.
I would like to make this 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;
}
}
}
You could try parallelism since it doesn't look like you need a synchronous treatment. A parallel foreach with PLINQ would do the trick.
But if you cannot wait until VS2010 is officially out, you could try Poor Man's Parallel.ForEach Iterator by Emre Aydinceren
The approach seems to be quiet fast. But from the string you have, I conclude that the name is usually smaller then the {URL}. You can use .LastIndexOf() method. I think it starts from the end of string
Let's say you do find an answer here. I think you need to consider what the "solution" is going to look like for the next guy that looks at your code.
I'll take more readable code vs. a couple milliseconds any day.
Have you tried this with Regex and/or use a stringbuilder instead of a string?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With