daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         for (int i = 0; i < 10; i++)
  9.         {
  10.             RunTest();
  11.         }
  12.     }
  13.  
  14.     private static void RunTest()
  15.     {
  16.         const long cnt = 1000000000;
  17.  
  18.         var time1 = Test1(cnt);
  19.         var time2 = Test2(cnt);
  20.  
  21.         Console.WriteLine($"Done in: {time1} vs {time2} diff: {time1 - time2}");
  22.     }
  23.  
  24.     private static TimeSpan Test1(long cnt)
  25.     {
  26.         var v = 1;
  27.         var timer = Stopwatch.StartNew();
  28.         for (long i = cnt; i != 0; i--)
  29.         {
  30.             v = RotateBit(v);
  31.         }
  32.         timer.Stop();
  33.  
  34.         return timer.Elapsed;
  35.     }
  36.  
  37.     private static TimeSpan Test2(long cnt)
  38.     {
  39.         var v = 1;
  40.         var timer = Stopwatch.StartNew();
  41.         for (long i = cnt; i != 0; i--)
  42.         {
  43.             v = RotateBit(v);
  44.         }
  45.         timer.Stop();
  46.  
  47.         return timer.Elapsed;
  48.     }
  49.  
  50.     private static int RotateBit(int v)
  51.     {
  52.         v = v << 1;
  53.  
  54.         if ((v & 0b0000_1111) == 0)
  55.         {
  56.             v = 1;
  57.         }
  58.  
  59.         return v;
  60.     }
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top