Guest User

linq

a guest
Jun 7th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. [CoreJob]
  2. public class Linq_Bench
  3. {
  4.     float[] arr;
  5.  
  6.     [IterationSetup]
  7.     public void IterationSetup()
  8.     {
  9.         arr = (new float[262144]).Select(x => 2.0f).ToArray(); //This is how you do this, right? ;)
  10.     }
  11.  
  12.     [Benchmark]
  13.     public float[] linqSelect()
  14.     {
  15.         return arr.Select(x => x * x).ToArray();
  16.     }
  17.  
  18.     [Benchmark]
  19.     public float[] loopInplace()
  20.     {
  21.         for(int i = 0; i < arr.Length; i++)
  22.         {
  23.             arr[i] = arr[i] * arr[i];
  24.         }
  25.         return arr;
  26.     }
  27.     [Benchmark]
  28.     public float linqSum()
  29.     {
  30.         return arr.Sum(x => x * x);
  31.     }
  32.     [Benchmark]
  33.     public float linqStupidSum()
  34.     {
  35.         float sum = 0;
  36.         arr.Select(x => (sum += x * x)).ToList();
  37.         return sum;
  38.     }
  39.     [Benchmark]
  40.     public float loopSum()
  41.     {
  42.         float sum = 0;
  43.         for (int i = 0; i < arr.Length; i++)
  44.         {
  45.             sum  += arr[i] * arr[i];
  46.         }
  47.         return sum;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment