Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataContractSerializer objects conversion performace improvement

DataContractSerializer objects conversion performace improvement

Scheduled Pinned Locked Moved C#
performancequestioncode-review
16 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I impeham

    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.

    B Offline
    B Offline
    BillWoodruff
    wrote on last edited by
    #2

    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 1 Reply Last reply
    0
    • B BillWoodruff

      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 Offline
      I Offline
      impeham
      wrote on last edited by
      #3

      I know how to measure time - thanks - this is not the issue here. I'm looking for someone with DataContractSerializer and serialization experience. Thanks.

      W 1 Reply Last reply
      0
      • I impeham

        I know how to measure time - thanks - this is not the issue here. I'm looking for someone with DataContractSerializer and serialization experience. Thanks.

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #4

        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

        I 1 Reply Last reply
        0
        • W Wayne Gaylard

          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

          I Offline
          I Offline
          impeham
          wrote on last edited by
          #5

          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.

          1 Reply Last reply
          0
          • I impeham

            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.

            R Offline
            R Offline
            Ravi Bhavnani
            wrote on last edited by
            #6

            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

            I 1 Reply Last reply
            0
            • R Ravi Bhavnani

              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

              I Offline
              I Offline
              impeham
              wrote on last edited by
              #7

              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.

              R 1 Reply Last reply
              0
              • I impeham

                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.

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #8

                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

                I 2 Replies Last reply
                0
                • R Ravi Bhavnani

                  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

                  I Offline
                  I Offline
                  impeham
                  wrote on last edited by
                  #9

                  i will look into it - thanks a lot :)

                  1 Reply Last reply
                  0
                  • R Ravi Bhavnani

                    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

                    I Offline
                    I Offline
                    impeham
                    wrote on last edited by
                    #10

                    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?

                    R 1 Reply Last reply
                    0
                    • I impeham

                      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?

                      R Offline
                      R Offline
                      Ravi Bhavnani
                      wrote on last edited by
                      #11

                      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.  Implementing Clone() is easily done by calling .NET's MemberwiseClone() for most properties.  You will find the effort required to do this small in comparison to the run-time performance gain. /ravi

                      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                      I 1 Reply Last reply
                      0
                      • R Ravi Bhavnani

                        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.  Implementing Clone() is easily done by calling .NET's MemberwiseClone() for most properties.  You will find the effort required to do this small in comparison to the run-time performance gain. /ravi

                        My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                        I Offline
                        I Offline
                        impeham
                        wrote on last edited by
                        #12

                        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.

                        R 1 Reply Last reply
                        0
                        • I impeham

                          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.

                          R Offline
                          R Offline
                          Ravi Bhavnani
                          wrote on last edited by
                          #13

                          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, /ravi

                          My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                          I 1 Reply Last reply
                          0
                          • R Ravi Bhavnani

                            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, /ravi

                            My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                            I Offline
                            I Offline
                            impeham
                            wrote on last edited by
                            #14

                            Unfortunately, i cannot add the XmlInclude attribute to my types, so using XmlSerializer won't work.

                            R 1 Reply Last reply
                            0
                            • I impeham

                              Unfortunately, i cannot add the XmlInclude attribute to my types, so using XmlSerializer won't work.

                              R Offline
                              R Offline
                              Ravi Bhavnani
                              wrote on last edited by
                              #15

                              OK. This[^] article suggests BinaryDataContractSerializer may be the way to go.  Not sure if you'd already seen this. /ravi

                              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                              I 1 Reply Last reply
                              0
                              • R Ravi Bhavnani

                                OK. This[^] article suggests BinaryDataContractSerializer may be the way to go.  Not sure if you'd already seen this. /ravi

                                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                                I Offline
                                I Offline
                                impeham
                                wrote on last edited by
                                #16

                                Thanks - i'll look into it

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups