﻿using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Omnius.Base;

namespace Omnius.Benchmark
{
    public partial class Benchmark
    {
        public void Xor()
        {
            Unsafe.LoadNativeMethods();
            int c1 = GetCount(0);
            Unsafe.LoadPureUnsafeMethods();
            int c2 = GetCount(0);

            var sb = new StringBuilder();

            sb.AppendLine("Native Xor:     " + c1);
            sb.AppendLine("PureUnsafe Xor: " + c2);

            Console.WriteLine(sb.ToString());

            int GetCount(int seed)
            {
                var random = new Random(seed);
                var sw = Stopwatch.StartNew();

                int count = 0;

                int length = 1024 * 256;
                var x = new byte[length];
                var y = new byte[length];
                var r = new byte[length];

                random.NextBytes(x);
                random.NextBytes(y);

                for (; ; )
                {
                    Unsafe.Xor(x, y, r);

                    count++;
                    if (sw.Elapsed.TotalSeconds > 10) break;
                }

                return count;
            }
        }
    }
}
