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

Fastest way to convert an object to a string

P: n/a
Hello,

I'm trying to speed up a piece of code that is causing performance issues
with our product. The problem is we are using serialization to convert the
object to a string, this is costing us performance degrade. Does anyone know
any better way to archive this which just not degrade performance. The code
is written in C#.

Thanks
Nov 29 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
The answer to your question depends mostly on: 1) how the string
relates to the object, and 2) how you're actually creating the string.

To tackle (1), I'm going to assume that if you're "serializing" and
your result is a string, that there's XML involved. I've never tried
StringBuilder vs. XmlWriter, but I'm going to guess that XmlWriter is
much more efficient for writing well-formatted XML. Implement
IXmlSerializable, write your code, profile it, and let me know if it's
still too slow.

For (2), there are a few tips and tricks with creating strings. There's
lots of literature on the Internet about C# string interning and how
the CLR treats strings. Read that first (RTFM). Then, avoid loops that
look like:

string myConcatenatedString = "";
foreach (string myString in myStringList)
{
myConcatenatedString += myString;
}

Learn to use the StringBuilder object, String.Format, and
String.Concat, along with their various details like IFormatProvider.
You may want to look into StringWriter or other stream-based devices.

Some more information on what exactly your code does would be helpful.
Stephan

Buddy Home wrote:
Hello,

I'm trying to speed up a piece of code that is causing performance issues
with our product. The problem is we are using serialization to convert the
object to a string, this is costing us performance degrade. Does anyone know
any better way to archive this which just not degrade performance. The code
is written in C#.

Thanks
Nov 29 '06 #2

P: n/a
Thanks for the reply,

First may I say that the object is converted to a byte array via
BinaryStream using serialization. I use the MemoryStream to store the data.

I'm already aware how StringBuilders works and know that using the String
object to manupulate the string is expensive.

My main question is that does anyone know a better way to do this.

Thanks,
"ssamuel" <ss*****@gmail.comwrote in message
news:11**********************@14g2000cws.googlegro ups.com...
The answer to your question depends mostly on: 1) how the string
relates to the object, and 2) how you're actually creating the string.

To tackle (1), I'm going to assume that if you're "serializing" and
your result is a string, that there's XML involved. I've never tried
StringBuilder vs. XmlWriter, but I'm going to guess that XmlWriter is
much more efficient for writing well-formatted XML. Implement
IXmlSerializable, write your code, profile it, and let me know if it's
still too slow.

For (2), there are a few tips and tricks with creating strings. There's
lots of literature on the Internet about C# string interning and how
the CLR treats strings. Read that first (RTFM). Then, avoid loops that
look like:

string myConcatenatedString = "";
foreach (string myString in myStringList)
{
myConcatenatedString += myString;
}

Learn to use the StringBuilder object, String.Format, and
String.Concat, along with their various details like IFormatProvider.
You may want to look into StringWriter or other stream-based devices.

Some more information on what exactly your code does would be helpful.
Stephan

Buddy Home wrote:
>Hello,

I'm trying to speed up a piece of code that is causing performance issues
with our product. The problem is we are using serialization to convert
the
object to a string, this is costing us performance degrade. Does anyone
know
any better way to archive this which just not degrade performance. The
code
is written in C#.

Thanks

Nov 29 '06 #3

P: n/a
I can't tell you if it's faster, but you may want to check out
BitConverter.ToString()...

On Nov 29, 2:41 pm, "Buddy Home" <BuddyH...@Home.comwrote:
Thanks for the reply,

First may I say that the object is converted to a byte array via
BinaryStream using serialization. I use the MemoryStream to store the data.

I'm already aware how StringBuilders works and know that using the String
object to manupulate the string is expensive.

My main question is that does anyone know a better way to do this.
Nov 29 '06 #4

P: n/a
Thanks,

BitConverter.ToString() has no support to pass an object.

Thanks,

"Michael Letterle" <mi**************@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
>I can't tell you if it's faster, but you may want to check out
BitConverter.ToString()...

On Nov 29, 2:41 pm, "Buddy Home" <BuddyH...@Home.comwrote:
>Thanks for the reply,

First may I say that the object is converted to a byte array via
BinaryStream using serialization. I use the MemoryStream to store the
data.

I'm already aware how StringBuilders works and know that using the String
object to manupulate the string is expensive.

My main question is that does anyone know a better way to do this.

Nov 29 '06 #5

P: n/a
So you're not serializing to a string. You're serializing to bytes.
BinaryStream and MemoryStream don't have anything to do with strings.

BitConverter is probably the way to go. If your problem is performance,
don't go looking for the easiest way to code it. As with any high-level
language, you're going to pay a price to use generic high-level
abstractions. If you're looking to make it easy, stick with binary
serialization and suffer the performance. If you're looking for
performance, write code to do it using BitConverter.

BitConverter does handle objects. Ints, bytes, and doubles are objects.
Your object is made of a collection of ints, bytes and doubles (and
maybe some other value types). If you're looking for performance, break
your object into its component parts, byte pack them, and profile your
code to see what's faster.

While on that note, you could do the same without BitConverter. You can
write loops that'll shift bits out of your value types and write those
into arrays of bytes. I'll guess that BitConverter is faster, but
you'll have to profile to find out.
Stephan
Buddy Home wrote:
Thanks,

BitConverter.ToString() has no support to pass an object.

Thanks,

"Michael Letterle" <mi**************@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
I can't tell you if it's faster, but you may want to check out
BitConverter.ToString()...

On Nov 29, 2:41 pm, "Buddy Home" <BuddyH...@Home.comwrote:
Thanks for the reply,

First may I say that the object is converted to a byte array via
BinaryStream using serialization. I use the MemoryStream to store the
data.

I'm already aware how StringBuilders works and know that using the String
object to manupulate the string is expensive.

My main question is that does anyone know a better way to do this.
Nov 29 '06 #6

P: n/a
Thanks for the reply,

I've just checked and the BitConvertor does not have any conversion from
object to string. My object is a reference type, its a class.

Example.

public class MyMessage
{
// Contains fields, properties and methods.
}

I'm currently serializing this class to a byte array.

Thanks,
"ssamuel" <ss*****@gmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
So you're not serializing to a string. You're serializing to bytes.
BinaryStream and MemoryStream don't have anything to do with strings.

BitConverter is probably the way to go. If your problem is performance,
don't go looking for the easiest way to code it. As with any high-level
language, you're going to pay a price to use generic high-level
abstractions. If you're looking to make it easy, stick with binary
serialization and suffer the performance. If you're looking for
performance, write code to do it using BitConverter.

BitConverter does handle objects. Ints, bytes, and doubles are objects.
Your object is made of a collection of ints, bytes and doubles (and
maybe some other value types). If you're looking for performance, break
your object into its component parts, byte pack them, and profile your
code to see what's faster.

While on that note, you could do the same without BitConverter. You can
write loops that'll shift bits out of your value types and write those
into arrays of bytes. I'll guess that BitConverter is faster, but
you'll have to profile to find out.
Stephan
Buddy Home wrote:
>Thanks,

BitConverter.ToString() has no support to pass an object.

Thanks,

"Michael Letterle" <mi**************@gmail.comwrote in message
news:11**********************@l12g2000cwl.googleg roups.com...
>I can't tell you if it's faster, but you may want to check out
BitConverter.ToString()...

On Nov 29, 2:41 pm, "Buddy Home" <BuddyH...@Home.comwrote:
Thanks for the reply,

First may I say that the object is converted to a byte array via
BinaryStream using serialization. I use the MemoryStream to store the
data.

I'm already aware how StringBuilders works and know that using the
String
object to manupulate the string is expensive.

My main question is that does anyone know a better way to do this.


Nov 29 '06 #7

P: n/a


"Buddy Home" <Bu*******@Home.comwrote in message
news:45**********************@news.zen.co.uk...
Thanks for the reply,

I've just checked and the BitConvertor does not have any conversion from
object to string. My object is a reference type, its a class.

Example.

public class MyMessage
{
// Contains fields, properties and methods.
}

I'm currently serializing this class to a byte array.
. . ..
Ok, so where is your performance problem?

Is is in serializing the object to a byte array?

Are you stuffing the byte array into a string? If so, how? Base64 encoding?
David
Nov 29 '06 #8

P: n/a
Buddy Home <Bu*******@Home.comwrote:
I'm trying to speed up a piece of code that is causing performance issues
with our product. The problem is we are using serialization to convert the
object to a string, this is costing us performance degrade. Does anyone know
any better way to archive this which just not degrade performance. The code
is written in C#.
Does it have to cope with *any* serializable object, or just ones of a
particular type or set of types? If you're only interested in a
specific set of types, just write specific ToString/FromString methods
which deal directly with the class rather than going through
serialization.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 29 '06 #9

P: n/a
Thanks for the reply,

By class consists of Hashtable, strings, integers and double.

We are using Serialization because of the Hashtable.

Any other ideas?

Thanks
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Buddy Home <Bu*******@Home.comwrote:
>I'm trying to speed up a piece of code that is causing performance issues
with our product. The problem is we are using serialization to convert
the
object to a string, this is costing us performance degrade. Does anyone
know
any better way to archive this which just not degrade performance. The
code
is written in C#.

Does it have to cope with *any* serializable object, or just ones of a
particular type or set of types? If you're only interested in a
specific set of types, just write specific ToString/FromString methods
which deal directly with the class rather than going through
serialization.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 30 '06 #10

P: n/a
Hello David,

I'm just Serialization a class and returning the byte array. This is the
cause of the performance.

Here is snippet of the code.

BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, theObject);
byte[] myByteArray = ms.GetBuffer();

Thanks,

"David Browne" <davidbaxterbrowne no potted me**@hotmail.comwrote in
message news:O0**************@TK2MSFTNGP02.phx.gbl...
>

"Buddy Home" <Bu*******@Home.comwrote in message
news:45**********************@news.zen.co.uk...
>Thanks for the reply,

I've just checked and the BitConvertor does not have any conversion from
object to string. My object is a reference type, its a class.

Example.

public class MyMessage
{
// Contains fields, properties and methods.
}

I'm currently serializing this class to a byte array.
. . ..

Ok, so where is your performance problem?

Is is in serializing the object to a byte array?

Are you stuffing the byte array into a string? If so, how? Base64
encoding?
David


Nov 30 '06 #11

P: n/a
Here is the code snippet.

BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, theObject);
byte[] myByteArray = ms.GetBuffer();

Thanks,

"Buddy Home" <Bu*******@Home.comwrote in message
news:45**********************@news.zen.co.uk...
Thanks for the reply,

By class consists of Hashtable, strings, integers and double.

We are using Serialization because of the Hashtable.

Any other ideas?

Thanks
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
>Buddy Home <Bu*******@Home.comwrote:
>>I'm trying to speed up a piece of code that is causing performance
issues
with our product. The problem is we are using serialization to convert
the
object to a string, this is costing us performance degrade. Does anyone
know
any better way to archive this which just not degrade performance. The
code
is written in C#.

Does it have to cope with *any* serializable object, or just ones of a
particular type or set of types? If you're only interested in a
specific set of types, just write specific ToString/FromString methods
which deal directly with the class rather than going through
serialization.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Nov 30 '06 #12

P: n/a
Buddy Home <Bu*******@Home.comwrote:
By class consists of Hashtable, strings, integers and double.

We are using Serialization because of the Hashtable.

Any other ideas?
Just because it's using a hashtable doesn't mean you can't do the
conversion to a string yourself. It will require some care, but that
doesn't mean it's not possible.

What type of object does the hashtable contain, for both keys and
values?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 30 '06 #13

P: n/a
In this example it just contains strings, surely enumurating through it
going to slow the process down.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Buddy Home <Bu*******@Home.comwrote:
>By class consists of Hashtable, strings, integers and double.

We are using Serialization because of the Hashtable.

Any other ideas?

Just because it's using a hashtable doesn't mean you can't do the
conversion to a string yourself. It will require some care, but that
doesn't mean it's not possible.

What type of object does the hashtable contain, for both keys and
values?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 30 '06 #14

P: n/a
Buddy Home <Bu*******@Home.comwrote:
In this example it just contains strings, surely enumurating through it
going to slow the process down.
Well, serialization has to enumerate through it somehow too - the
difference is that you know in advance that you're going to be using
strings eventually, so you can optimise for that case.

You'll need to be careful to escape things appropriately etc, but it's
still likely to be a lot faster than serializing to binary and then
converting that to text.

I might look at creating a string serialization framework in my
miscutil library...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 30 '06 #15

P: n/a
KH <KH@discussions.microsoft.comwrote:
From the other replies it doesn't seem like we're understanding exactly what
you're trying to do - can you be more specific as to where you're starting
and exactly what you want to do; maybe even a small example program? I mean,
for example, the original object isn't resident in memory to start with,
you're reading it from a file or something right?

If you can serialize it as a String (rather than its own type), or make it
implicitly convertible to string, you might look at using the Encoding class
which has methods to create a string from bytes in a particular encoding
(which would be UNICODE/UTF16 if serialized as System.String)
Converting arbitrary binary data to a string shouldn't be done with the
Encoding class - that way lies to "invalid" strings, losses of data
etc.

The safe was is to use Convert.ToBase64String and
Convert.FromBase64String.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 1 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.