.NET Forum / Languages / C# / November 2006
Fastest way to convert an object to a string
|
|
Thread rating:  |
Buddy Home - 29 Nov 2006 18:47 GMT 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
ssamuel - 29 Nov 2006 19:00 GMT 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
> Hello, > [quoted text clipped - 5 lines] > > Thanks Buddy Home - 29 Nov 2006 19:41 GMT 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,
> 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. [quoted text clipped - 37 lines] >> >> Thanks Michael Letterle - 29 Nov 2006 19:46 GMT I can't tell you if it's faster, but you may want to check out BitConverter.ToString()...
> Thanks for the reply, > [quoted text clipped - 5 lines] > > My main question is that does anyone know a better way to do this. Buddy Home - 29 Nov 2006 19:57 GMT Thanks,
BitConverter.ToString() has no support to pass an object.
Thanks,
>I can't tell you if it's faster, but you may want to check out > BitConverter.ToString()... [quoted text clipped - 9 lines] >> >> My main question is that does anyone know a better way to do this. ssamuel - 29 Nov 2006 20:18 GMT 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
> Thanks, > [quoted text clipped - 15 lines] > >> > >> My main question is that does anyone know a better way to do this. Buddy Home - 29 Nov 2006 21:26 GMT 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,
> So you're not serializing to a string. You're serializing to bytes. > BinaryStream and MemoryStream don't have anything to do with strings. [quoted text clipped - 39 lines] >> >> >> >> My main question is that does anyone know a better way to do this. David Browne - 29 Nov 2006 22:16 GMT > Thanks for the reply, > [quoted text clipped - 10 lines] > 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
Buddy Home - 30 Nov 2006 19:36 GMT 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,
>> Thanks for the reply, >> [quoted text clipped - 19 lines] > > David Jon Skeet [C# MVP] - 29 Nov 2006 22:30 GMT > 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.
 Signature Jon Skeet - <skeet@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
Buddy Home - 30 Nov 2006 19:33 GMT 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
>> 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 [quoted text clipped - 10 lines] > which deal directly with the class rather than going through > serialization. Buddy Home - 30 Nov 2006 19:36 GMT Here is the code snippet.
BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, theObject); byte[] myByteArray = ms.GetBuffer();
Thanks,
> Thanks for the reply, > [quoted text clipped - 20 lines] >> which deal directly with the class rather than going through >> serialization. Jon Skeet [C# MVP] - 30 Nov 2006 20:01 GMT > 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?
 Signature Jon Skeet - <skeet@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
Buddy Home - 30 Nov 2006 21:41 GMT In this example it just contains strings, surely enumurating through it going to slow the process down.
>> By class consists of Hashtable, strings, integers and double. >> [quoted text clipped - 8 lines] > What type of object does the hashtable contain, for both keys and > values? Jon Skeet [C# MVP] - 30 Nov 2006 21:53 GMT > 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...
 Signature Jon Skeet - <skeet@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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|