Optimizing C# String Processing

G

Guest

Hello,

I would like to maximize efficiency of string processing in my C#
application that works with Skype API. Actually, these "strings" are made of
"chars" that are all 8-bit values except 0 (1-255).

1. Is there a way to make C# use 8-bit char so that the memory usage is
reduced?

2. Should StringBuilder be used in place of string while they are being
processed? I haven't done it so far since StringBuilder doesn't have Trim,
StartWith, Substring, & Split functions that I use extensively. Would it be
more efficient to use StringBuilder and convert to string for these missing
functions?

3. Is there an encoding that can efficiently convert byte value (0-255) into
non-zero byte values (1-255)?

4. Are there any other things I should be looking for to raise efficiency?

Regards,
Vinay Agarwal
 
M

Mattias Sjögren

Actually, these "strings" are made of
"chars" that are all 8-bit values except 0 (1-255).

Just the fact that you put strings in quotes is a clear indication
that you're using the wrong data type.

1. Is there a way to make C# use 8-bit char so that the memory usage is
reduced?

No, but why aren't you using a byte array instead of strring/char?

2. Should StringBuilder be used in place of string while they are being
processed?

That's usually a good idea, yes.


Mattias
 
H

Helge Jensen

Vinay said:
Hello,

I would like to maximize efficiency of string processing in my C#
application that works with Skype API. Actually, these "strings" are made of
"chars" that are all 8-bit values except 0 (1-255).

Are you actually having any efficiency problems? Does the profiler
indicate so?
1. Is there a way to make C# use 8-bit char so that the memory usage is
reduced?

How large are those strings? you may be able to "save" half the mem by
using a byte[] representation, but really, are you having memory problems?
2. Should StringBuilder be used in place of string while they are being
processed? I haven't done it so far since StringBuilder doesn't have Trim,
StartWith, Substring, & Split functions that I use extensively. Would it be
more efficient to use StringBuilder and convert to string for these missing
functions?

"While they are being processed"?

StringBuilder *builds* strings, and is good if you concatenate many
strings together, but if you are calling StartWith, Substring & Split,
you are not concatenating.
3. Is there an encoding that can efficiently convert byte value (0-255) into
non-zero byte values (1-255)?

You are not suggesting to complicate your memory representation by
compressing from the domain 1-255 into 0-255, are you?
 
J

Jon Skeet [C# MVP]

Vinay Agarwal said:
I would like to maximize efficiency of string processing in my C#
application that works with Skype API. Actually, these "strings" are made of
"chars" that are all 8-bit values except 0 (1-255).

Hmm... are they *actually* text characters, or are you really just
representing binary data?
1. Is there a way to make C# use 8-bit char so that the memory usage is
reduced?

Well, you could use a byte array instead - but then you can't treat it
like a string. You could write your own class which had string-like
facilities, but if you needed to display it (or the like) you'd need to
convert it to a string.
2. Should StringBuilder be used in place of string while they are being
processed? I haven't done it so far since StringBuilder doesn't have Trim,
StartWith, Substring, & Split functions that I use extensively. Would it be
more efficient to use StringBuilder and convert to string for these missing
functions?

StringBuilder can help if you don't need the intermediate values, but
it's no use otherwise.
3. Is there an encoding that can efficiently convert byte value (0-255) into
non-zero byte values (1-255)?

Well, you'll need some sort of escaping, effectively - for instance,
making 255 255 represent 255, but 255 1 represent 0, leaving all other
values as they are. It's not going to be terribly pleasant either way.
4. Are there any other things I should be looking for to raise efficiency?

Well, firstly, do you know that you've got a problem? It's not worth
wasting time and potentially making your code less elegant if you don't
have a performance problem in real life.

Secondly, if you're actually dealing with binary data, don't try to
treat it like a string - you'll end up with really nasty bugs that way.
 
G

Guest

Sorry for the late response, I was interrupted by other tasks. From your
feedbad, I understand that I need to look into optimization as a whole and
not focus on strings only. I just started looking into profiler tools--not
sure which is the best, but I will start out with dotTrace. Thanks for your
help.

Regards,
Vinay Agarwal
 

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

Top