By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,308 Members | 2,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,308 IT Pros & Developers. It's quick & easy.

Optimizing C# String Processing

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

P: n/a


Vinay Agarwal wrote:
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?

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3

P: n/a
Vinay Agarwal <Vi**********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
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
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.