Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. public class MathMaxBenchmarks
  2. {
  3. private static double[] _testInput;
  4.  
  5. [GlobalSetup]
  6. public void Setup()
  7. {
  8. _testInput = Enumerable.Range(0, 10000).Select(i => (double)i).ToArray();
  9. }
  10.  
  11. [Benchmark(Baseline = true)]
  12. public double[] MathMax()
  13. {
  14. double[] output = new double[_testInput.Length];
  15. for (var i = 0; i < _testInput.Length; i++)
  16. output[i] = Math.Max(_testInput[i], 100);
  17. return output;
  18. }
  19.  
  20. [Benchmark]
  21. public double[] Ternary()
  22. {
  23. double[] output = new double[_testInput.Length];
  24. for (var i = 0; i < _testInput.Length; i++)
  25. {
  26. double tmp = _testInput[i];
  27. output[i] = tmp > 100.0 ? tmp : 100.0;
  28. }
  29. return output;
  30. }
  31.  
  32. [Benchmark]
  33. public double[] vmaxsd()
  34. {
  35. var limit = Vector128.Create(100.0);
  36.  
  37. double[] output = new double[_testInput.Length];
  38. for (var i = 0; i < _testInput.Length; i++)
  39. output[i] = Sse2.MaxScalar(Vector128.CreateScalarUnsafe(_testInput[i]), limit).ToScalar();
  40. return output;
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement