public void CreateObjPerfTest() { //FastCreateInstance(typeof(colclass)); //lambdaCreateInstance(typeof(colclass)); int count = 100000; Console.WriteLine("count = " + count.ToString("#,#")); var w = new System.Diagnostics.Stopwatch(); w.Start(); for (int i = 0; i < count; i++) { object o = new colclass(); object s = new Retstruct(); } w.Stop(); Console.WriteLine("normal new T() time ms = " + w.ElapsedMilliseconds); w.Restart(); for (int i = 0; i < count; i++) { object o = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(colclass)); object s = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(Retstruct)); } w.Stop(); Console.WriteLine("FormatterServices time ms = " + w.ElapsedMilliseconds); w.Restart(); for (int i = 0; i < count; i++) { object o = FastCreateInstance(typeof(colclass)); object s = FastCreateInstance(typeof(Retstruct)); } w.Stop(); Console.WriteLine("IL newobj (FastCreateInstance) time ms = " + w.ElapsedMilliseconds); w.Restart(); for (int i = 0; i < count; i++) { object o = lambdaCreateInstance(typeof(colclass)); object s = lambdaCreateInstance(typeof(Retstruct)); } w.Stop(); Console.WriteLine("lambda time ms = " + w.ElapsedMilliseconds); w.Restart(); for (int i = 0; i < count; i++) { object o = Activator.CreateInstance(typeof(colclass)); object s = Activator.CreateInstance(typeof(Retstruct)); } w.Stop(); Console.WriteLine("Activator.CreateInstance time ms = " + w.ElapsedMilliseconds); Console.WriteLine("The Activaor.CreateInstance method appears to be faster than FastCreateInstance in this test.\nIts implementation caches the most recently used 16 types.\nIf we are to serialize more than 16 types, its performance will not be as good as FastCreateInstance."); }
public void StructTest() { Retstruct r = new Retstruct(); r.Name = "hello"; r.Field1 = "dsasdF"; r.Field2 = 2312; r.date = DateTime.Now; #if !SILVERLIGHT r.ds = CreateDataset().Tables[0]; #endif var s = JSON.ToJSON(r); Console.WriteLine(s); var o = JSON.ToObject(s); Assert.IsNotNull(o); Assert.AreEqual(2312, ((Retstruct)o).Field2); }