tags:

views:

521

answers:

8

Hello, does any of you know what would be better:

a. get a string s, convert to char array and loop over it, or

b. get a string s, loop over substrings of it (s.Substring(i, 1))?

Any tips much appreciated.

+2  A: 

it would be quicker to profile both routines with some large strings than research and / or wait for answers, but if I had to guess I'd wager A

Hardryv
+1  A: 

b Sounds odd. Just guessing, but a sounds quicker, and certainly more understandable.

EDIT: Hence, if you want the index, yer old

for (int i = 0; i < s.Length; i++)
  //Do something with s[i]

will do fine

Or, for some LINQ overkill

s.Select((i, c) => //i being index, c the char, or the other way round, I forget at times);
flq
+11  A: 

Option b), looping over substrings, is very inefficient.

The fastest method would be

c) loop over the string chars directly, using the read-only indexer property:

for (int i = 0; i < s.Length; i++) { char c = s[i]; ... }

or, based on the IEnumerable< char> interface:

foreach(char c in s) { ... }
Henk Holterman
+4  A: 

(A) is much more efficient. But why not just do:

foreach (char c in s)

Some quick benchmarks indicate that this is a tiny bit (<2% difference) less efficient than

for(int i = 0; i < s.Length; ++i)
    char c = s[i];

But my opinion is that the readability of the former makes it superior for anything but the most time-critical code.

Matt Howells
I remember... I can't. The indices are relevant. That is why I chose to say "looping" instead of "iterating".
Dervin Thunk
int index = 0; foreach(char c in s){ CODE index++; }
luiscubal
@luis: Nah... too much.
Dervin Thunk
My understanding is that foreach is almost always a performance hit, regardless of it's innate flexibility.
Hardryv
Added comparison with indexing.
Matt Howells
+3  A: 

It depends what you are trying to do.

Do you just need to examine characters at particular indexes, or create a new string based on them or what?

Remember SubString() will return a new string. This may cause unwanted overhead depending on what you are trying to do. A clearer explanation of the problem would help.

Also, don't pre-optimize. Write your code whichever way you feel more productive. Then profile it and address this issue if it causes a bottleneck.

Winston Smith
"don't pre-optimize": Right, problem is I'm trying to implement a given algorithms I've proved correct, and I want to write it as efficiently as possible. It will not cause a bottleneck anywhere but on itself, so, I'm not really *pre*-optimazing. Just optimizing.
Dervin Thunk
If you're that far along, then you can run it both ways and measure the difference. And evaluate whether it makes a difference, and if it does, select an approach based on that measurement.
Carl Manaster
@Carl: Yes, you're probably right. My C# coding skills lag well behind my math skills... not even sure how to profile other than stopwatch...
Dervin Thunk
@Dervin there are a few free profilers out there. Try EQATEC. I haven't used it, but I've heard it's good. There's also one by JetBrains called dotTrace. It isn't free, but you can download a trial version.
Winston Smith
That's great to know. Thanks, Joe.
Dervin Thunk
+1  A: 

Why convert the string to a char array? There's an indexer on the String class that lets you get the individual characters:

char c = s[i];
Michael Burr
A: 

What exactly are you doing? When there is an optimized method for the string operation you are performing, it will tremendously outperform any kind of loop you come up with. As a simple example:

File.ReadAllLines(...);
...
Regex exp = new Regex(..., RegexOptions.Compiled);
foreach (line)
    exp.Match(...);

is much slower than:

File.ReadAllText(...);
...
foreach (match in Regex.Match(..., RegexOptions.Multiline))
    ...
280Z28
A: 

While using substring remember that it will gives you the new string the orignal string will remain as it is.Wgy i am saying that orignal string will remain as it is because .NET interns its strings, so future instances will pull the same immutable string from the intern pool, and any performance hit will be negligible. In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool. The single copy of each string is called its 'intern' and is typically looked up by a method of the string class. So by using substring every time you are creating new string So instead of substring choose the for loop option.

string str = "TestString";
for (int i = 0; i < str.Length; i++)
{
    char c = str[i];
}
Raghav Khunger