Example #1
0
        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.");
        }
Example #2
0
        public static colclass CreateObject()
        {
            var c = new colclass();

            c.booleanValue    = true;
            c.ordinaryDecimal = 3;

            if (exotic)
            {
                c.nullableGuid = Guid.NewGuid();
#if !SILVERLIGHT
                c.hash = new Hashtable();
                c.hash.Add(new class1("0", "hello", Guid.NewGuid()), new class2("1", "code", "desc"));
                c.hash.Add(new class2("0", "hello", "pppp"), new class1("1", "code", Guid.NewGuid()));
                if (dsser)
                {
                    c.dataset = ds;
                }
#endif
                c.bytes            = new byte[1024];
                c.stringDictionary = new Dictionary <string, baseclass> ();
                c.objectDictionary = new Dictionary <baseclass, baseclass> ();
                c.intDictionary    = new Dictionary <int, baseclass> ();
                c.nullableDouble   = 100.003;


                c.nullableDecimal = 3.14M;



                c.stringDictionary.Add("name1", new class2("1", "code", "desc"));
                c.stringDictionary.Add("name2", new class1("1", "code", Guid.NewGuid()));

                c.intDictionary.Add(1, new class2("1", "code", "desc"));
                c.intDictionary.Add(2, new class1("1", "code", Guid.NewGuid()));

                c.objectDictionary.Add(new class1("0", "hello", Guid.NewGuid()), new class2("1", "code", "desc"));
                c.objectDictionary.Add(new class2("0", "hello", "pppp"), new class1("1", "code", Guid.NewGuid()));

                c.arrayType    = new baseclass[2];
                c.arrayType[0] = new class1();
                c.arrayType[1] = new class2();
            }


            c.items.Add(new class1("1", "1", Guid.NewGuid()));
            c.items.Add(new class2("2", "2", "desc1"));
            c.items.Add(new class1("3", "3", Guid.NewGuid()));
            c.items.Add(new class2("4", "4", "desc2"));

            c.laststring = "" + DateTime.Now;

            return(c);
        }
Example #3
0
        public void Speed_Test_Serialize()
        {
            Console.Write("fastjson serialize");
            //fastJSON.JSON.Parameters.UsingGlobalTypes = false;
            colclass c = CreateObject();
            double   t = 0;

            for (int pp = 0; pp < tcount; pp++)
            {
                DateTime st       = DateTime.Now;
                string   jsonText = null;
                for (int i = 0; i < count; i++)
                {
                    jsonText = Json.ToJson(c);
                }
                t += DateTime.Now.Subtract(st).TotalMilliseconds;
                Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds);
            }
            Console.WriteLine("\tAVG = " + t / tcount);
        }
Example #4
0
        public void Speed_Test_Deserialize()
        {
            Console.Write("fastjson deserialize");
            colclass c = CreateObject();
            double   t = 0;

            for (int pp = 0; pp < tcount; pp++)
            {
                DateTime st = DateTime.Now;
                colclass deserializedStore;
                string   jsonText = Json.ToJson(c);
                //Console.WriteLine(" size = " + jsonText.Length);
                for (int i = 0; i < count; i++)
                {
                    deserializedStore = (colclass)Json.ToObject(jsonText);
                }
                t += DateTime.Now.Subtract(st).TotalMilliseconds;
                Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds);
            }
            Console.WriteLine("\tAVG = " + t / tcount);
        }