TheBulgarianWolf

A Branch Prediction

Nov 23rd, 2020
1,695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace ABranchPrediction
  7. {
  8.     class Program
  9.     {
  10.         const int size = 10_000_000;
  11.         static int[] data = new int[size];
  12.  
  13.         static void GenerateRandomData()
  14.         {
  15.             Random rand = new Random();
  16.             for (int i = 0; i < size; i++)
  17.             {
  18.                 data[i] = rand.Next(10);
  19.             }
  20.         }
  21.  
  22.         static int CountBoundary(int boundary)
  23.         {
  24.             int result = 0;
  25.             for (int i = 0; i < size; i++)
  26.             {
  27.                 if (data[i] < boundary)
  28.                 {
  29.                     result++;
  30.                 }
  31.             }
  32.             return result;
  33.         }
  34.         static void Main(string[] args)
  35.         {
  36.             GenerateRandomData();
  37.             for (int i = 0; i < 11; i++)
  38.             {
  39.                 Stopwatch watch = new Stopwatch();
  40.                 watch.Start();
  41.                 int count = CountBoundary(i);
  42.                 watch.Stop();
  43.                 Console.WriteLine( $"Boundary{i}: Count = {count}, Elapsed = {watch.ElapsedTicks}");
  44.             }
  45.  
  46.             Console.WriteLine("Ready. Press ENTER to quit.");
  47.             Console.ReadLine();
  48.         }
  49.     }
  50. }
  51.  
Add Comment
Please, Sign In to add comment