Guest User

Untitled

a guest
Jul 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Configs;
  3. using BenchmarkDotNet.Exporters;
  4. using BenchmarkDotNet.Jobs;
  5. using BenchmarkDotNet.Running;
  6. using ConsoleApp4.Base;
  7. using ConsoleApp4.RealArray;
  8.  
  9. namespace ConsoleApp4
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. BenchmarkRunner.Run<BenchMark>();
  16.  
  17. //Console.WriteLine(new BenchMark().Base());
  18. //Console.WriteLine(new BenchMark().RealArrayGen());
  19. }
  20. }
  21.  
  22. public class BenchmarkConfig : ManualConfig
  23. {
  24. public BenchmarkConfig()
  25. {
  26. Add(MarkdownExporter.GitHub);
  27. Add(Job.ShortRun);
  28. }
  29. }
  30.  
  31. [Config(typeof(BenchmarkConfig))]
  32. public class BenchMark
  33. {
  34. [Benchmark(Baseline = true)]
  35. public float Base()
  36. {
  37. float sum = 0.0f;
  38.  
  39. for (int i = 0; i < 100000; i++)
  40. {
  41. var a = new NdArrayF.NdArray(new[] { 1.0f, 2.0f });
  42. var b = new NdArrayF.NdArray(new[] { 3.0f, 4.0f });
  43.  
  44. var c = a + b;
  45. var d = b - a;
  46.  
  47. sum += c[0] + d[0] + c[1] + d[1];
  48. }
  49.  
  50. return sum;
  51. }
  52.  
  53. [Benchmark]
  54. public float RealArrayGen()
  55. {
  56. float sum = 0.0f;
  57. Function<float, Calc> function = new Function<float, Calc>();
  58.  
  59. for (int i = 0; i < 100000; i++)
  60. {
  61. var a = new NdArrayRealGen.NdArray<float, Calc>(new[] { 1.0f, 2.0f });
  62. var b = new NdArrayRealGen.NdArray<float, Calc>(new[] { 3.0f, 4.0f });
  63.  
  64. var c = a + b;
  65. var d = b - a;
  66.  
  67. sum += function.Sum(c, d);
  68. }
  69.  
  70. return sum;
  71. }
  72.  
  73. [Benchmark]
  74. public float RealArrayIn()
  75. {
  76. float sum = 0.0f;
  77. FunctionF function = new FunctionF();
  78.  
  79. for (int i = 0; i < 100000; i++)
  80. {
  81. var a = new NdArrayRealGen.NdArrayF(new[] { 1.0f, 2.0f });
  82. var b = new NdArrayRealGen.NdArrayF(new[] { 3.0f, 4.0f });
  83.  
  84. var c = a + b;
  85. var d = b - a;
  86.  
  87. sum += function.Sum(c, d);
  88. }
  89.  
  90. return sum;
  91. }
  92.  
  93. }
  94.  
  95. class FunctionF : Function<float, Calc> { }
  96.  
  97. class Function<T, TCalc> where T : struct where TCalc : struct, ICalc<T>
  98. {
  99. public T Sum(NdArrayRealGen.NdArray<T, TCalc> x, NdArrayRealGen.NdArray<T, TCalc> y)
  100. {
  101. return x[0] + y[0] + x[1] + y[1];
  102. }
  103. }
  104.  
  105. namespace Base
  106. {
  107. public class NdArrayF
  108. {
  109. public class NdArray
  110. {
  111. private float[] _data;
  112. public int Length => _data.Length;
  113.  
  114. public float this[int index]
  115. {
  116. get { return _data[index]; }
  117. set { _data[index] = value; }
  118. }
  119.  
  120. public NdArray(float[] data)
  121. {
  122. float[] result = new float[data.Length];
  123.  
  124. for (int i = 0; i < data.Length; i++)
  125. {
  126. result[i] = data[i];
  127. }
  128.  
  129. _data = result;
  130. }
  131.  
  132. public static NdArray operator +(NdArray x, NdArray y)
  133. {
  134. float[] result = new float[x.Length];
  135.  
  136. for (int i = 0; i < result.Length; i++)
  137. {
  138. result[i] = x._data[i] + y._data[i];
  139. }
  140.  
  141. return new NdArray(result);
  142. }
  143.  
  144. public static NdArray operator -(NdArray x, NdArray y)
  145. {
  146. float[] result = new float[x.Length];
  147.  
  148. for (int i = 0; i < result.Length; i++)
  149. {
  150. result[i] = x._data[i] - y._data[i];
  151. }
  152.  
  153. return new NdArray(result);
  154. }
  155. }
  156. }
  157. }
  158.  
  159. namespace RealArray
  160. {
  161. public class NdArrayRealGen
  162. {
  163. public class NdArrayF : NdArray<float, Calc>
  164. {
  165. public NdArrayF(float[] data) : base(data)
  166. {
  167. }
  168. }
  169.  
  170. public class NdArrayD: NdArray<double, Calc>
  171. {
  172. public NdArrayD(double[] data) : base(data)
  173. {
  174. }
  175. }
  176.  
  177. public class NdArray<T, TCalc> where T : struct where TCalc : struct, ICalc<T>
  178. {
  179. private readonly Real<T, TCalc>[] _data;
  180. public int Length => _data.Length;
  181.  
  182. public Real<T, TCalc> this[int index]
  183. {
  184. get { return _data[index]; }
  185. set { _data[index] = value; }
  186. }
  187.  
  188. public NdArray(T[] data)
  189. {
  190. Real<T, TCalc>[] result = new Real<T, TCalc>[data.Length];
  191.  
  192. for (int i = 0; i < data.Length; i++)
  193. {
  194. result[i] = data[i];
  195. }
  196.  
  197. _data = result;
  198. }
  199.  
  200. public static NdArray<T, TCalc> operator +(NdArray<T, TCalc> x, NdArray<T, TCalc> y)
  201. {
  202. T[] result = new T[x.Length];
  203.  
  204. for (int i = 0; i < result.Length; i++)
  205. {
  206. result[i] = x._data[i] + y._data[i];
  207. }
  208.  
  209. return new NdArray<T, TCalc>(result);
  210. }
  211.  
  212. public static NdArray<T, TCalc> operator -(NdArray<T, TCalc> x, NdArray<T, TCalc> y)
  213. {
  214. T[] result = new T[x.Length];
  215.  
  216. for (int i = 0; i < result.Length; i++)
  217. {
  218. result[i] = x._data[i] - y._data[i];
  219. }
  220.  
  221. return new NdArray<T, TCalc>(result);
  222. }
  223. }
  224. }
  225.  
  226. public interface ICalc<T>
  227. {
  228. T Add(T x, T y);
  229. T Sub(T x, T y);
  230. }
  231.  
  232. public struct Calc : ICalc<double>, ICalc<float>
  233. {
  234. public double Add(double x, double y)
  235. {
  236. return x + y;
  237. }
  238.  
  239. public double Sub(double x, double y)
  240. {
  241. return x - y;
  242. }
  243.  
  244. public float Add(float x, float y)
  245. {
  246. return x + y;
  247. }
  248.  
  249. public float Sub(float x, float y)
  250. {
  251. return x - y;
  252. }
  253. }
  254.  
  255. public struct Real<T, TCalc> where T : struct where TCalc : struct, ICalc<T>
  256. {
  257. private T _value;
  258.  
  259. private Real(T value)
  260. {
  261. this._value = value;
  262. }
  263.  
  264. public static T operator +(Real<T, TCalc> x, Real<T, TCalc> y)
  265. {
  266. return default(TCalc).Add(x._value, y._value);
  267. }
  268.  
  269. public static T operator -(Real<T, TCalc> x, Real<T, TCalc> y)
  270. {
  271. return default(TCalc).Sub(x._value, y._value);
  272. }
  273.  
  274. public static T operator +(T x, Real<T, TCalc> y)
  275. {
  276. return default(TCalc).Add(x, y._value);
  277. }
  278.  
  279. public static T operator -(T x, Real<T, TCalc> y)
  280. {
  281. return default(TCalc).Sub(x, y._value);
  282. }
  283.  
  284. public static T operator +(Real<T, TCalc> x, T y)
  285. {
  286. return default(TCalc).Add(x._value, y);
  287. }
  288.  
  289. public static T operator -(Real<T, TCalc> x, T y)
  290. {
  291. return default(TCalc).Sub(x._value, y);
  292. }
  293.  
  294. public static implicit operator Real<T, TCalc>(T val)
  295. {
  296. return new Real<T, TCalc>(val);
  297. }
  298.  
  299. public static implicit operator T(Real<T, TCalc> real)
  300. {
  301. return real._value;
  302. }
  303.  
  304. public override string ToString()
  305. {
  306. return this._value.ToString();
  307. }
  308. }
  309.  
  310. }
  311. }
Add Comment
Please, Sign In to add comment