DataContractSerializer objects conversion performace improvement
-
I think it may be valuable for you if you give more detail on what type of object conversion you are performing here. Measuring elapsed time using DateTime is well-known to be "limited" in accuracy. Starting with .NET 2.0, you can use the 'StopWatch in the System.Diagnostics library timer to get more accurate results: [^]. Executing the code several times before starting a timing operation is also believed to improve timing accuracy by eliminating any possible one-time costs of getting the code "jitted." There are several articles here on CodeProject dealing with accurate timing: just search.
“The best hope is that one of these days the Ground will get disgusted enough just to walk away ~ leaving people with nothing more to stand ON than what they have so bloody well stood FOR up to now.” Kenneth Patchen, Poet
-
I know how to measure time - thanks - this is not the issue here. I'm looking for someone with DataContractSerializer and serialization experience. Thanks.
There was no need to get short with Bill. He was only trying to give you a bit of good advice. Using a DateTime to measure code timing, clearly shows you do not know how to measure time.
Everyone dies - but not everyone lives
-
There was no need to get short with Bill. He was only trying to give you a bit of good advice. Using a DateTime to measure code timing, clearly shows you do not know how to measure time.
Everyone dies - but not everyone lives
Sorry - didn't mean to insult in no way - However, measuring is not the issue here - i am quite sure with the results since the times of the operations are long (~10 seconds) because the objects contains a lot of data - it is not milliseconds of difference.
-
I am using the following code to convert from one type of object to another. It works fine, but I am looking for a way to improve performance:
public static object ConvertObject(object source, Type targetType) { object result; Type sourceType = source.GetType(); DataContractSerializer sourceSerializer = new DataContractSerializer(sourceType); using (MemoryStream ms = new MemoryStream()) { DateTime start = DateTime.Now; sourceSerializer.WriteObject(ms, source); TimeSpan writeObjectTime = DateTime.Now - start; ms.Seek(0, SeekOrigin.Begin); DataContractSerializer targetSerializer = new DataContractSerializer(targetType); XmlReader r = XmlReader.Create(ms); DynamicObjectSerializer.MyReader myreader = new DynamicObjectSerializer.MyReader(r); start = DateTime.Now; result = targetSerializer.ReadObject(myreader); TimeSpan readObjectTime = DateTime.Now - start; LoggerHolder.LogDebug("\\"{0}\\" Write Time = {1}, Read Time = {2}", source.GetType().Name, writeObjectTime, readObjectTime); } return result; }
Using the MemoryStream actually slows the process down since the object is first being written to it and then read from it. If it was possible to serialize the source object to the target one directly it would have been taken half the time. Any idea how this can be done? Thanks.
You should consider using Fasterflect[^] or Automapper[^]. Your current implementation is hugely malperformant. [Edit] And you're really mapping between objects, not converting types, right? [/Edit] /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
You should consider using Fasterflect[^] or Automapper[^]. Your current implementation is hugely malperformant. [Edit] And you're really mapping between objects, not converting types, right? [/Edit] /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
Object converting - right. The problem is that i want to convert objects without knowing its members - it should be done using their names (one property data in object one is copied to object two's property which has the same name). using serialization i can serialize one object from one module to an object of another module easily. I was looking for a way to serialize from one object to another without using a memory stream in the middle - that will solve my problem.
-
Object converting - right. The problem is that i want to convert objects without knowing its members - it should be done using their names (one property data in object one is copied to object two's property which has the same name). using serialization i can serialize one object from one module to an object of another module easily. I was looking for a way to serialize from one object to another without using a memory stream in the middle - that will solve my problem.
impeham wrote:
it should be done using their names
That's exactly what I thought you wanted to do. You're mapping (not converting) objects. Use Automapper instead of serialization. It's orders of magnitude faster. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
impeham wrote:
it should be done using their names
That's exactly what I thought you wanted to do. You're mapping (not converting) objects. Use Automapper instead of serialization. It's orders of magnitude faster. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
impeham wrote:
it should be done using their names
That's exactly what I thought you wanted to do. You're mapping (not converting) objects. Use Automapper instead of serialization. It's orders of magnitude faster. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
After checking AutoMapper i realized that i need to specifically map all internal types of the objects from source to target. this requires as my objects can contain many types and it will require a LOT of work (not to mention maintenance as they change) before it can be used and i need something to do the conversions automatically with minimal work => leads me to my first posted code. So i return to the original question - is it possible to skip the MemoryStream read/write?
-
After checking AutoMapper i realized that i need to specifically map all internal types of the objects from source to target. this requires as my objects can contain many types and it will require a LOT of work (not to mention maintenance as they change) before it can be used and i need something to do the conversions automatically with minimal work => leads me to my first posted code. So i return to the original question - is it possible to skip the MemoryStream read/write?
impeham wrote:
is it possible to skip the MemoryStream read/write?
Only if you write mappings for each internal object. My philosophy is to build
Clone()
methods for each object (class), and call them for nested objects as required. ImplementingClone()
is easily done by calling .NET'sMemberwiseClone()
for most properties. You will find the effort required to do this small in comparison to the run-time performance gain. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
impeham wrote:
is it possible to skip the MemoryStream read/write?
Only if you write mappings for each internal object. My philosophy is to build
Clone()
methods for each object (class), and call them for nested objects as required. ImplementingClone()
is easily done by calling .NET'sMemberwiseClone()
for most properties. You will find the effort required to do this small in comparison to the run-time performance gain. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
That will take a lot of work for implementation & maintenance afterwards which i am trying to avoid. I will have to think about this more. Thanks.
If you have a generalized object graph and a need to map any type of object to any other type, I would like to suggest that you revisit your design. Object mapping is mostly used when converting DTOs to business objects, and frameworks like Automapper and Fasterflect prove to be very convenient (and fast) in these cases. If you choose to go with your existing design, you may want to consider
XmlSerializer
which caches serializers for different types, causing a one-time performance hit on first convert. But this is also not as performant as simple mapping. Cheers, /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
If you have a generalized object graph and a need to map any type of object to any other type, I would like to suggest that you revisit your design. Object mapping is mostly used when converting DTOs to business objects, and frameworks like Automapper and Fasterflect prove to be very convenient (and fast) in these cases. If you choose to go with your existing design, you may want to consider
XmlSerializer
which caches serializers for different types, causing a one-time performance hit on first convert. But this is also not as performant as simple mapping. Cheers, /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Unfortunately, i cannot add the XmlInclude attribute to my types, so using XmlSerializer won't work.
-