Question about string efficiency

  • Thread starter Thread starter Mark Baldwin
  • Start date Start date
M

Mark Baldwin

Bearing in mind the overhead of handling strings in .NET, which is more
efficient...

// trim once but use temporary
string s1 = textBox1.Text.Trim();
if (s1.Length > 0)
s2 = s1;

OR

// trim twice with no temporary
if (textBox1.Text.Trim().Length > 0)
s2 = textBox1.Text.Trim();
 
Hi Mark,

first is more preferable because:

1. String has obvious meaning (if called other then "s1")
2. You can reuse the result
2.5. hence .Trim() is called only once

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com



MB> Bearing in mind the overhead of handling strings in .NET, which is
MB> more efficient...
MB>
MB> // trim once but use temporary
MB> string s1 = textBox1.Text.Trim();
MB> if (s1.Length > 0)
MB> s2 = s1;
MB> OR
MB>
MB> // trim twice with no temporary
MB> if (textBox1.Text.Trim().Length > 0)
MB> s2 = textBox1.Text.Trim();
 
Bearing in mind the overhead of handling strings in .NET, which is more
efficient...

// trim once but use temporary
string s1 = textBox1.Text.Trim();
if (s1.Length > 0)
s2 = s1;

OR

// trim twice with no temporary
if (textBox1.Text.Trim().Length > 0)
s2 = textBox1.Text.Trim();

The former is certainly more efficient in almost every situation. The
extra reference on the stack is very unlikely to be significant in the
slightest. (It's unlikely that an extra call to Trim will make much
difference either, but there we go.)

I'd personally say the first version is more readable too, but that's
a judgement call. It's likely to be more important than the efficiency
aspect though...

Jon
 
Hi,


Mark Baldwin said:
Bearing in mind the overhead of handling strings in .NET, which is more
efficient...

// trim once but use temporary
string s1 = textBox1.Text.Trim();
if (s1.Length > 0)
s2 = s1;

OR

// trim twice with no temporary
if (textBox1.Text.Trim().Length > 0)
s2 = textBox1.Text.Trim();

Unless that this happens in a loop, I would say forget about it and move on.
The difference between them is going to be negligible.

The first most be more efficient, you are only consuming a little more
memory.

I find more readable the second though.
 
Bearing in mind the overhead of handling strings in .NET, which is more
efficient...

// trim once but use temporary
string s1 = textBox1.Text.Trim();
if (s1.Length > 0)
s2 = s1;

OR

// trim twice with no temporary
if (textBox1.Text.Trim().Length > 0)
s2 = textBox1.Text.Trim();


There will be no appreciable difference unless you (frequently) plan on calling this many millions of times. Don't worry about IOW. Worry about what's cleanest in code. For legibility you might then start to build your own generic libary starting with these perhaps:


public static bool IsWhiteSpace(string stringIn)
{
Debug.Assert(stringIn != null);

int length = stringIn.Length;
for (int i = 0; i < length; ++i)
{
if (!char.IsWhiteSpace(stringIn, i))
{
return false;
}
}

return true;
}

public static bool IsWhiteSpace(TextBox textbox)
{
Debug.Assert(textbox != null);

return IsWhiteSpace(textBox.Text);
}
 
// trim once but use temporary
string s1 = textBox1.Text.Trim();
if (s1.Length > 0)
s2 = s1;

OR

// trim twice with no temporary
if (textBox1.Text.Trim().Length > 0)
s2 = textBox1.Text.Trim();

Duplication is almost always bad in programming. What's bad about the second
example is that the two duplicate expressions are bound to each other - if
one changes, you have to remember to change the other one. Duplication can
also introduce inefficiency if it's an operation that's duplicated, as in
this case.

The second example might be a teensy bit faster if the string is always
empty (which it probably isn't, in this case).

///ark
 
Mark Baldwin said:
Bearing in mind the overhead of handling strings in .NET, which is more
efficient...

// trim once but use temporary
string s1 = textBox1.Text.Trim();
if (s1.Length > 0)
s2 = s1;

OR

// trim twice with no temporary
if (textBox1.Text.Trim().Length > 0)
s2 = textBox1.Text.Trim();

The first one, hands down. There's no extra cost from the first one
whatsoever. The second one does have a temporary as well, it just isn't
named.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top