Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BenchmarkDotNet.Attributes;
- using BenchmarkDotNet.Configs;
- using BenchmarkDotNet.Exporters;
- using BenchmarkDotNet.Jobs;
- using BenchmarkDotNet.Running;
- using ConsoleApp4.Base;
- using ConsoleApp4.RealArray;
- namespace ConsoleApp4
- {
- class Program
- {
- static void Main(string[] args)
- {
- BenchmarkRunner.Run<BenchMark>();
- //Console.WriteLine(new BenchMark().Base());
- //Console.WriteLine(new BenchMark().RealArrayGen());
- }
- }
- public class BenchmarkConfig : ManualConfig
- {
- public BenchmarkConfig()
- {
- Add(MarkdownExporter.GitHub);
- Add(Job.ShortRun);
- }
- }
- [Config(typeof(BenchmarkConfig))]
- public class BenchMark
- {
- [Benchmark(Baseline = true)]
- public float Base()
- {
- float sum = 0.0f;
- for (int i = 0; i < 100000; i++)
- {
- var a = new NdArrayF.NdArray(new[] { 1.0f, 2.0f });
- var b = new NdArrayF.NdArray(new[] { 3.0f, 4.0f });
- var c = a + b;
- var d = b - a;
- sum += c[0] + d[0] + c[1] + d[1];
- }
- return sum;
- }
- [Benchmark]
- public float RealArrayGen()
- {
- float sum = 0.0f;
- Function<float, Calc> function = new Function<float, Calc>();
- for (int i = 0; i < 100000; i++)
- {
- var a = new NdArrayRealGen.NdArray<float, Calc>(new[] { 1.0f, 2.0f });
- var b = new NdArrayRealGen.NdArray<float, Calc>(new[] { 3.0f, 4.0f });
- var c = a + b;
- var d = b - a;
- sum += function.Sum(c, d);
- }
- return sum;
- }
- [Benchmark]
- public float RealArrayIn()
- {
- float sum = 0.0f;
- FunctionF function = new FunctionF();
- for (int i = 0; i < 100000; i++)
- {
- var a = new NdArrayRealGen.NdArrayF(new[] { 1.0f, 2.0f });
- var b = new NdArrayRealGen.NdArrayF(new[] { 3.0f, 4.0f });
- var c = a + b;
- var d = b - a;
- sum += function.Sum(c, d);
- }
- return sum;
- }
- }
- class FunctionF : Function<float, Calc> { }
- class Function<T, TCalc> where T : struct where TCalc : struct, ICalc<T>
- {
- public T Sum(NdArrayRealGen.NdArray<T, TCalc> x, NdArrayRealGen.NdArray<T, TCalc> y)
- {
- return x[0] + y[0] + x[1] + y[1];
- }
- }
- namespace Base
- {
- public class NdArrayF
- {
- public class NdArray
- {
- private float[] _data;
- public int Length => _data.Length;
- public float this[int index]
- {
- get { return _data[index]; }
- set { _data[index] = value; }
- }
- public NdArray(float[] data)
- {
- float[] result = new float[data.Length];
- for (int i = 0; i < data.Length; i++)
- {
- result[i] = data[i];
- }
- _data = result;
- }
- public static NdArray operator +(NdArray x, NdArray y)
- {
- float[] result = new float[x.Length];
- for (int i = 0; i < result.Length; i++)
- {
- result[i] = x._data[i] + y._data[i];
- }
- return new NdArray(result);
- }
- public static NdArray operator -(NdArray x, NdArray y)
- {
- float[] result = new float[x.Length];
- for (int i = 0; i < result.Length; i++)
- {
- result[i] = x._data[i] - y._data[i];
- }
- return new NdArray(result);
- }
- }
- }
- }
- namespace RealArray
- {
- public class NdArrayRealGen
- {
- public class NdArrayF : NdArray<float, Calc>
- {
- public NdArrayF(float[] data) : base(data)
- {
- }
- }
- public class NdArrayD: NdArray<double, Calc>
- {
- public NdArrayD(double[] data) : base(data)
- {
- }
- }
- public class NdArray<T, TCalc> where T : struct where TCalc : struct, ICalc<T>
- {
- private readonly Real<T, TCalc>[] _data;
- public int Length => _data.Length;
- public Real<T, TCalc> this[int index]
- {
- get { return _data[index]; }
- set { _data[index] = value; }
- }
- public NdArray(T[] data)
- {
- Real<T, TCalc>[] result = new Real<T, TCalc>[data.Length];
- for (int i = 0; i < data.Length; i++)
- {
- result[i] = data[i];
- }
- _data = result;
- }
- public static NdArray<T, TCalc> operator +(NdArray<T, TCalc> x, NdArray<T, TCalc> y)
- {
- T[] result = new T[x.Length];
- for (int i = 0; i < result.Length; i++)
- {
- result[i] = x._data[i] + y._data[i];
- }
- return new NdArray<T, TCalc>(result);
- }
- public static NdArray<T, TCalc> operator -(NdArray<T, TCalc> x, NdArray<T, TCalc> y)
- {
- T[] result = new T[x.Length];
- for (int i = 0; i < result.Length; i++)
- {
- result[i] = x._data[i] - y._data[i];
- }
- return new NdArray<T, TCalc>(result);
- }
- }
- }
- public interface ICalc<T>
- {
- T Add(T x, T y);
- T Sub(T x, T y);
- }
- public struct Calc : ICalc<double>, ICalc<float>
- {
- public double Add(double x, double y)
- {
- return x + y;
- }
- public double Sub(double x, double y)
- {
- return x - y;
- }
- public float Add(float x, float y)
- {
- return x + y;
- }
- public float Sub(float x, float y)
- {
- return x - y;
- }
- }
- public struct Real<T, TCalc> where T : struct where TCalc : struct, ICalc<T>
- {
- private T _value;
- private Real(T value)
- {
- this._value = value;
- }
- public static T operator +(Real<T, TCalc> x, Real<T, TCalc> y)
- {
- return default(TCalc).Add(x._value, y._value);
- }
- public static T operator -(Real<T, TCalc> x, Real<T, TCalc> y)
- {
- return default(TCalc).Sub(x._value, y._value);
- }
- public static T operator +(T x, Real<T, TCalc> y)
- {
- return default(TCalc).Add(x, y._value);
- }
- public static T operator -(T x, Real<T, TCalc> y)
- {
- return default(TCalc).Sub(x, y._value);
- }
- public static T operator +(Real<T, TCalc> x, T y)
- {
- return default(TCalc).Add(x._value, y);
- }
- public static T operator -(Real<T, TCalc> x, T y)
- {
- return default(TCalc).Sub(x._value, y);
- }
- public static implicit operator Real<T, TCalc>(T val)
- {
- return new Real<T, TCalc>(val);
- }
- public static implicit operator T(Real<T, TCalc> real)
- {
- return real._value;
- }
- public override string ToString()
- {
- return this._value.ToString();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment