Mounika

Mounika

  • NA
  • 93
  • 10.3k

At Serialization in ObjecttoByteArray() exe getting slow

Jun 23 2016 9:00 AM
namespace ConsoleApplication2
{
[Serializable]
struct DataTypes
{
public byte a;
public byte b;
public byte c;
public byte d;
public byte e;
public byte f;
public byte g;
public byte h;
public byte i;
public byte j;
public byte k;
public UInt16 l;
public UInt16 m;
public UInt16 n;
public UInt16 o;
public UInt16 p;
public UInt16 q;
public UInt16 r;
public UInt16 s;
public UInt16 t;
public UInt16 u;
public UInt16 v;
public UInt16 w;
public UInt16 x;
public UInt16 y;
public UInt16 z;
public UInt16 a1;
public UInt16 a2;
public UInt16 a3;
public UInt16 a4;
};
[Serializable]
struct Cycle_
{
public Int16 A;
public Int16 B;
public Int16 C;
public Int16 D;
public Int32 E;
};
class declaration
{
public List<Cycle_> cycledata = new List<Cycle_>();
public List<DataTypes> idata = new List<DataTypes>();
public byte[] ObjectToByteArray(Object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
}
class Program
{
static void Main(string[] args)
{
declaration dec = new declaration();
DataTypes ion;
Cycle_ cycle;
for(int i=0;i<1000000;i++)
{
ion.a = 0;
ion.b = 1;
ion.c = 2;
ion.d = 3;
ion.e = 4;
ion.f = 5;
ion.g = 6;
ion.h = 7;
ion.i = 8;
ion.j = 9;
ion.k = 10;
ion.l = 11;
ion.m = 12;
ion.n = 13;
ion.o = 14;
ion.p = 15;
ion.q = 16;
ion.r = 17;
ion.s = 18;
ion.t = 19;
ion.u = 20;
ion.v = 21;
ion.w = 22;
ion.x = 23;
ion.y = 24;
ion.z = 25;
ion.a1 = 26;
ion.a2 = 27;
ion.a3 = 28;
ion.a4 = 29;
cycle.A = 40;
cycle.B = 41;
cycle.C = 42;
cycle.D = 43;
cycle.E = 44;
dec.cycledata.Add(cycle);
dec.idata.Add(ion);
}
byte[] arrByte = dec.ObjectToByteArray(dec.cycledata);
byte[] arrByte1 = dec.ObjectToByteArray(dec.idata);
}
}
}
 
 
 
 
In objecttoByteArray Function in declaration class... Serialization will done for converting list to byte array , at that point exe getting slow for some few milliseconds.