Click here to Skip to main content
15,886,757 members
Articles / Programming Languages / C#

AltSerializer - An Alternate Binary Serializer

Rate me:
Please Sign up or sign in to vote.
4.93/5 (27 votes)
19 Sep 20066 min read 141.4K   1.9K   69  
The AltSerializer is a replacement for the binary serializer built in to .NET.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

using AltSerialize;

namespace AltSerializeTest.Benchmark
{
    public class BenchmarkTest
    {
        private int _iterations = 500000;
        public int Iterations
        {
            get { return _iterations; }
            set { _iterations = value; }
        }

        private int _serializeTime;
        public int SerializeTime
        {
            get { return _serializeTime; }
            set { _serializeTime = value; }
        }

        private int _deserializeTime;
        public int DeserializeTime
        {
            get { return _deserializeTime; }
            set { _deserializeTime = value; }
        }

        private int StartTime;
        private int StopTime;
        private int TotalTime;
        
        
        protected void StartTimer()
        {
            StartTime = Environment.TickCount;
        }

        protected void StopTimer()
        {
            StopTime = Environment.TickCount;
            TotalTime = StopTime - StartTime;
        }

        protected void PrintStats()
        {
            double sMs = ((double)SerializeTime / 1000.0);
            double dMs = ((double)DeserializeTime / 1000.0);
            Console.WriteLine("Serialize time: " + SerializeTime + " (" + sMs.ToString() + " s)");
            Console.WriteLine("Deserialize time: " + DeserializeTime + " (" + dMs.ToString() + " s)");
        }

        public void TestCompiledObject()
        {
            byte[] bytes = null;

            SimpleCompiledClass compClass = new SimpleCompiledClass();
            bytes = Serializer.Serialize(compClass); 
            StartTimer();
            for (int i = 0; i < Iterations; i++)
            {
                bytes = Serializer.Serialize(compClass, typeof(SimpleCompiledClass));
            }
            StopTimer();
            SerializeTime = TotalTime;

            StartTimer();
            for (int i = 0; i < Iterations; i++)
            {
                object obj = Serializer.Deserialize(bytes, typeof(SimpleCompiledClass));
            }
            StopTimer();
            DeserializeTime = TotalTime;

            PrintStats();
        }

        public void TestDefaultObject()
        {
            BinaryFormatter fmt = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
           
            SimpleClass compClass = new SimpleClass();
            StartTimer();
            for (int i = 0; i < Iterations; i++)
            {
                ms.Position = 0;
                fmt.Serialize(ms, compClass);
            }
            StopTimer();
            SerializeTime = TotalTime;

            StartTimer();
            for (int i = 0; i < Iterations; i++)
            {
                ms.Position = 0;
                object obj = fmt.Deserialize(ms);
            }
            StopTimer();
            DeserializeTime = TotalTime;

            PrintStats();
        }

        public void TestObject()
        {
            byte[] bytes = null;

            SimpleClass compClass = new SimpleClass();
            StartTimer();
            for (int i = 0; i < Iterations; i++)
            {
                bytes = Serializer.Serialize(compClass, typeof(SimpleClass));
            }
            StopTimer();
            SerializeTime = TotalTime;

            StartTimer();
            for (int i = 0; i < Iterations; i++)
            {
                object obj = Serializer.Deserialize(bytes, typeof(SimpleClass));
            }
            StopTimer();
            DeserializeTime = TotalTime;

            PrintStats();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I've been writing code since I started learning BASIC on the Apple //c. Since then, I've learned C, C++, a few assembly languages. I went to college for Computer Science and Mathematics at the University of Texas at Dallas.

Comments and Discussions