title: Serialization taking too much time
body: <p>Here is code which takes almost 3 to 8 sec for serialization depends on object type. I want to store this result into Redis for caching. But this operation is taking too long. Also same for deserialization.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public byte[] SerializeObject(object objectToSerialize)
  {
      try
      {
          //If object to serialize is null then return null
          if (objectToSerialize == null)
              return null;
  
          byte[] result;
          //Create memory stream and use it for Serializing object
          using (var ms = new MemoryStream())
          {
              using (var zs = new GZipStream(ms, CompressionMode.Compress, true))
              {
                  var bf = new BinaryFormatter();
                  bf.Serialize(zs, objectToSerialize);
              }
              result = ms.ToArray();
          }
          return result;
      }
      catch (Exception ex)
      {
          //Some code
          return null;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Updated: How I have tried serializing a <code>DataTable</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using(var client = m_oRedisClientsManager.GetClient()) {
      //Serialize DataTable to byte
      byteCachedDatatable = m_oSerializer.SerializeDataTable(oCacheObject);
  
      //add Serialized bytes to redis and update expiration time
      client.Set(sCacheKey, byteCachedDatatable, new TimeSpan(0, iExpiryTimeInMins, 0));
  }
  
  public byte[] SerializeDataTable(DataTable dataTable) {
      if (dataTable == null) return null;
      byte[] result;
      using(var memoryStream = new MemoryStream()) {
          using(var deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress)) {
              dataTable.WriteXml(deflateStream, XmlWriteMode.WriteSchema);
              deflateStream.Flush();
              deflateStream.Close();
              result = memoryStream.ToArray();
          }
      }
      return result;
  }
  </code></pre>
  
  <p>Any pointers will be helpful.</p>