Ejemplo n.º 1
0
        private static void DoBSParallel <T>(T obj, int iterations)
        {
            var stopwatch = new Stopwatch();

            var ser = new BinaryDataSerialization.BinaryDataSerializer();

            stopwatch.Start();
            var data = Enumerable.Range(0, iterations).AsParallel().Select(i =>
            {
                using (var ms = new MemoryStream())
                {
                    ser.Serialize(ms, obj);
                    return(ms.ToArray());
                }
            }).ToArray();

            stopwatch.Stop();
            Console.WriteLine("BS || SER: {0}", stopwatch.Elapsed);
            stopwatch.Reset();

            stopwatch.Start();
            data.AsParallel().ForAll(d => ser.Deserialize <T>(d));
            stopwatch.Stop();
            Console.WriteLine("BS || DESER: {0}", stopwatch.Elapsed);
            stopwatch.Reset();
        }
Ejemplo n.º 2
0
        private static async Task DoBSAsync <T>(T obj, int iterations)
        {
            var stopwatch = new Stopwatch();

            var ser = new BinaryDataSerialization.BinaryDataSerializer();

            using (var ms = new MemoryStream())
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    ser.Serialize(ms, obj);
                }
                stopwatch.Stop();
                Console.WriteLine("BSA SER: {0}", stopwatch.Elapsed);
                stopwatch.Reset();
            }

            var dataStream = new MemoryStream();

            ser.Serialize(dataStream, obj);
            byte[] data = dataStream.ToArray();

            using (var ms = new MemoryStream(data))
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    await ser.DeserializeAsync <T>(ms)
                    .ConfigureAwait(false);

                    ms.Position = 0;
                }
                stopwatch.Stop();
                Console.WriteLine("BSA DESER: {0}", stopwatch.Elapsed);
                stopwatch.Reset();
            }
        }